如何使用powershell在csv文件中选择所需的列

时间:2017-04-24 06:56:18

标签: powershell csv

我有一个csv文件,数据看起来像这样。

Div,Date,HomeTeam,AwayTeam,FTHG,FTAG,FTR,HTHG,HTAG,HTR
I1,20/08/16,Juventus,Fiorentina,2,1,H,1,0,H
I1,20/08/16,Roma,Udinese,4,0,H,0,0,D
I1,21/08/16,Atalanta,Lazio,3,4,A,0,3,A
I1,21/08/16,Bologna,Crotone,1,0,H,0,0,D

我正在尝试将下面的前六列导出到新的csv文件中。

Div,Date,HomeTeam,AwayTeam,FTHG,FTAG
I1,20/08/16,Juventus,Fiorentina,2,1
I1,20/08/16,Roma,Udinese,4,0
I1,21/08/16,Atalanta,Lazio,3,4
I1,21/08/16,Bologna,Crotone,1,0

我正在使用以下powershell命令。

Import-Csv $infile -DeLimiter ","|
Select'Div','Date','HomeTeam','AwayTeam','FTHG','FTAG'| 
Export-Csv $trimfile -DeLimiter "," -NoTypeInformation

但我的输出csv文件看起来像这样

"Div","Date","HomeTeam","AwayTeam","FTHG","FTAG"
"I1","20/08/16","Juventus","Fiorentina","2","1"
"I1","20/08/16","Roma","Udinese","4","0"
"I1","21/08/16","Atalanta","Lazio","3","4"
"I1","21/08/16","Bologna","Crotone","1","0"

实际文件中的no.of列要高得多。在这里简化它。我做错了什么?

1 个答案:

答案 0 :(得分:2)

尝试使用此代码:

Import-Csv $infile -DeLimiter "," | 
Select-Object 'Div','Date','HomeTeam','AwayTeam','FTHG','FTAG' | 
ConvertTo-Csv -NoTypeInformation | 
ForEach-Object { $_ -replace '"', ""} | 
Out-File $trimfile