我有一个包含多行的文件,每行有5个部分,用逗号分隔。我正在尝试拆分这些行,并在一行上返回同一行中的多个部分。我的尝试是这样的:
Get-Content $file | ForEach-Object { $_.split(",")[0,1] }不幸的是,它在单独的行上返回每个元素。如果在PowerShell中还有其他工作方式,我对此持开放态度。
发布于 2014-10-23 15:02:12
这?
Get-Content $file | ForEach-Object { -join $_.split(",")[0,1] }发布于 2014-10-23 15:15:07
如果您的输入是CSV,为什么不使用Import-Csv并创建一个对象。
[file.csv]
this,is,the,first,line
this,is,the,second,line然后,
$csv = Import-Csv 'C:\tmp\file.csv' -Header One,Two,Three,Four,Five这为您提供了:
PS> $csv | Format-Table -AutoSize
One Two Three Four Five
--- --- ----- ---- ----
this is the first line
this is the second line现在拿上你需要的任何东西:
PS> $csv[0].Four + ' ' + $csv[0].Five
first line
PS> 0..$csv.Count | % {$csv[$_].Four + ' ' + $csv[$_].Five}
first line
second linehttps://stackoverflow.com/questions/26520692
复制相似问题