PowerShell:不带标题的Import-CSV,并删除部分重复的行 [英] PowerShell: Import-CSV with no headers and remove partial duplicate lines

查看:456
本文介绍了PowerShell:不带标题的Import-CSV,并删除部分重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日志文件,格式为CSV,没有标题.第一列基本上是所记录问题的唯一标识符.对于同一问题标识符,可能有多行具有不同的详细信息.我想删除第一列重复的行,因为此时我不需要其他数据.

I have a log file that is formatted as a CSV with no headers. The first column is basically the unique identifier for the issues being recorded. There may be multiple lines with different details for the same issue identifier. I would like to remove lines where the first column is duplicated because I don't need the other data at this time.

到目前为止,我对PowerShell已有相当的基础知识,所以我确定我缺少一些简单的东西.

I have fairly basic knowledge of PowerShell at this point, so I'm sure there's something simple I'm missing.

很抱歉,如果重复的话,但是我可以找到一些问题来回答问题的某些部分,而不是整个问题.

I'm sorry if this is a duplicate, but I could find questions to answer some parts of the question, but not the question as a whole.

到目前为止,我最好的猜测是:

So far, my best guess is:

Import-Csv $outFile | % { Select-Object -Index 1 -Unique } | Out-File $outFile -Append

但这给了我错误:

Import-Csv:成员"LB"已经存在. 在C:\ Users \ jnurczyk \ Desktop \ Scratch \ POImport \ getPOImport.ps1:6 char:1 + Import-Csv $ outFile | %{Select-Object -InputObject $ _ -Index 1 -Unique} |出去 ... + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo:未指定:(:) [Import-Csv],ExtendedTypeSystemException + FullyQualifiedErrorId:AlreadyPresentPSMemberInfoInternalCollectionAdd,Microsoft.PowerShell.Commands.ImportCsvCommand

Import-Csv : The member "LB" is already present. At C:\Users\jnurczyk\Desktop\Scratch\POImport\getPOImport.ps1:6 char:1 + Import-Csv $outFile | % { Select-Object -InputObject $_ -Index 1 -Unique } | Out ... + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Import-Csv], ExtendedTypeSystemException + FullyQualifiedErrorId : AlreadyPresentPSMemberInfoInternalCollectionAdd,Microsoft.PowerShell.Commands.ImportCsvCommand

推荐答案

由于数据没有标题,因此需要在Import-Csv cmdlet中指定标题.然后,如果要使用第一列仅选择唯一记录,则需要在Select-Object cmdlet中进行指定.参见下面的代码:

Because your data has no headers, you need to specify the headers in your Import-Csv cmdlet. And then to select only unique records using the first column, you need to specify that in the Select-Object cmdlet. See code below:

Import-Csv $outFile -Header A,B,C | Select-Object -Unique A

为澄清起见,我的示例中的标题分别是A,B和C.如果您知道有多少列,则可以使用此标题.如果标题太少,则会删除列.如果标题太多,那么它们将成为空字段.

To clarify, the headers in my example are A, B, and C. This works if you know how many columns there are. If you have too few headers, then columns are dropped. If you have too many headers, then they become empty fields.

这篇关于PowerShell:不带标题的Import-CSV,并删除部分重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆