仅从csv文件中删除重复记录

时间:2016-04-28 17:24:03

标签: powershell powershell-v3.0

我尝试使用谷歌搜索不同的东西,但我似乎无法找到答案。

Csv内容

"ColumnOne","ColumnTwo","ColumnThree"
"C1V1","C2V1","C3V1"
"C1V2","C2V2","C3V2"
"C1V2","C2V2","C3V2"
"C1V3","C2V3","C3V3"

代码

$input = 'C:\Temp\test.csv'
$inputCsv = Import-Csv $input | Select-Object -Unique

$inputCsv | Export-Csv "$input-temp.csv" -NoTypeInformation
#Move-Item "$input-temp.csv" $input -Force

输出

"ColumnOne","ColumnTwo","ColumnThree"
"C1V2","C2V2","C3V2"

预期输出

"ColumnOne","ColumnTwo","ColumnThree"
"C1V1","C2V1","C3V1"
"C1V2","C2V2","C3V2"
"C1V3","C2V3","C3V3"

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:3)

您需要使用Select-Object或Sort-Object指定列名称以获得唯一结果。如果您想要排序,请尝试以下代码。如果您不想,请使用Select-Object。

$input = 'C:\Temp\test.csv'
$inputCsv = Import-Csv $input | Sort-Object * -Unique

$inputCsv | Export-Csv "$input-temp.csv" -NoTypeInformation

您还可以参考博客HERE