过滤/删除CSV文件的最后一列

时间:2018-08-20 07:09:45

标签: powershell

我需要过滤CSV行的最后一个元素。因此,在每个文件的最后一列。 据我了解,我进行了此导入,以过滤并导出最后一列。

(Import-Csv Test.csv -Delimiter "," -Header "R","S","T","U","V","W","X","Y","Z","A") |
    Select-Object -ExcludeProperty $_.A |
    Export-Csv test1.csv –NoTypeInformation  

然后,我的导出文件由一个重复的标头组成,其中添加了,,,,,,+ +每行也添加了,,,,,,。

是否有更好的方法过滤或删除最后一列?

编辑:这是我的输入文件:

R,"S","T","U","V","W","X","Y","Z","A"
R,"Software","Test","Uruguay","22","Waldschutzinitiative","Xander","Ytho","Zahlung","AJ3DJF"
RwieRichard,"S","Test","Uruguay","22","Waldschutzinitiative","Xander","Ytho","Zahlung","AJ3DJF"
RwieRichard,"Software","T","Uruguay","22","Waldschutzinitiative","Xander","Ytho","Zahlung","AJ3DJF"
RwieRichard,"Software","Test","U","22","Waldschutzinitiative","Xander","Ytho","Zahlung","AJ3DJF"
RwieRichard,"Software","Test","Uruguay","V","Waldschutzinitiative","Xander","Ytho","Zahlung","AJ3DJF"
RwieRichard,"Software","Test","Uruguay","22","We","Xander","Ytho","Zahlung","AJ3DJF"
RwieRichard,"Software","Test","Uruguay","22","Waldschutzinitiative","X","Ytho","Zahlung","AJ3DJF"
RwieRichard,"Software","Test","Uruguay","22","Waldschutzinitiative","Xander","Y","Zahlung","AJ3DJF"
RwieRichard,"Software","Test","Uruguay","22","Waldschutzinitiative","Xander","Ytho","Z","AJ3DJF"
RwieRichard,"Software","Test","Uruguay","22","Waldschutzinitiative","Xander","Ytho","Zahlung","A"
RwieRichard,"Software","Test","Uruguay","22","Waldschutzinitiative","Xander","Ytho","Zahlung","AJ3DJF"

输出文件:

"R,""S"",""T"",""U"",""V"",""W"",""X"",""Y"",""Z"""
"R,""S"",""T"",""U"",""V"",""W"",""X"",""Y"",""Z"",""A"",,,,,,,,"
"R,""Software"",""Test"",""Uruguay"",""22"",""Waldschutzinitiative"",""Xander"",""Ytho"",""Zahlung"",""AJ3DJF"",,,,,,,,"
"RwieRichard,""S"",""Test"",""Uruguay"",""22"",""Waldschutzinitiative"",""Xander"",""Ytho"",""Zahlung"",""AJ3DJF"",,,,,,,,"
"RwieRichard,""Software"",""T"",""Uruguay"",""22"",""Waldschutzinitiative"",""Xander"",""Ytho"",""Zahlung"",""AJ3DJF"",,,,,,,,"
"RwieRichard,""Software"",""Test"",""U"",""22"",""Waldschutzinitiative"",""Xander"",""Ytho"",""Zahlung"",""AJ3DJF"",,,,,,,,"
"RwieRichard,""Software"",""Test"",""Uruguay"",""V"",""Waldschutzinitiative"",""Xander"",""Ytho"",""Zahlung"",""AJ3DJF"",,,,,,,,"
"RwieRichard,""Software"",""Test"",""Uruguay"",""22"",""We"",""Xander"",""Ytho"",""Zahlung"",""AJ3DJF"",,,,,,,,"
"RwieRichard,""Software"",""Test"",""Uruguay"",""22"",""Waldschutzinitiative"",""X"",""Ytho"",""Zahlung"",""AJ3DJF"",,,,,,,,"
"RwieRichard,""Software"",""Test"",""Uruguay"",""22"",""Waldschutzinitiative"",""Xander"",""Y"",""Zahlung"",""AJ3DJF"",,,,,,,,"
"RwieRichard,""Software"",""Test"",""Uruguay"",""22"",""Waldschutzinitiative"",""Xander"",""Ytho"",""Z"",""AJ3DJF"",,,,,,,,"
"RwieRichard,""Software"",""Test"",""Uruguay"",""22"",""Waldschutzinitiative"",""Xander"",""Ytho"",""Zahlung"",""A"",,,,,,,,"
"RwieRichard,""Software"",""Test"",""Uruguay"",""22"",""Waldschutzinitiative"",""Xander"",""Ytho"",""Zahlung"",""AJ3DJF"",,,,,,,,"

1 个答案:

答案 0 :(得分:1)

要使-ExcludeProperty工作,您还必须指定-Property。另外,该参数的参数必须仅是属性名称,而不是$_.A

更改

... | Select-Object -ExcludeProperty $_.A | ...

... | Select-Object -Property * -ExcludeProperty A | ...

编辑:从您声称的示例输出中可以看出,实际代码和数据之间的分隔符不匹配。您的数据是以逗号分隔的,并且您为导入指定了一个不同的定界符,或者您确实为导入指定了逗号,但是您的实际数据由另一个字符定界。无论哪种方式,都可以解决。如果您的数据实际上用逗号分隔,则您甚至不需要为导入指定分隔符,因为Import-Csv默认为,

此外,您的数据似乎已经具有标题,因此在导入过程中一定不要指定标题。

如果以逗号分隔,这应该正确导入您的CSV:

Import-Csv 'test.csv' | ...

如果以分号分隔,这应该正确导入CSV:

Import-Csv 'test.csv' -Delimiter ';' | ...