2014-10-23 2 views
0

각 줄마다 쉼표로 구분 된 다섯 개의 섹션이있는 여러 줄의 파일이 있습니다. 나는이 줄을 나누어 한 줄에 같은 줄에서 여러 구역을 반환하려고합니다. 내 시도는 다음과 같습니다.

Get-Content $file | ForEach-Object { $_.split(",")[0,1] } 

불행히도, 각 요소는 별도의 줄에 반환됩니다. PowerShell 내에서 작동하는 또 다른 방법이 있다면, 나는 그것에 대해 개방적입니다.

답변

0

this?

Get-Content $file | ForEach-Object { -join $_.split(",")[0,1] } 
0

입력 내용이 CSV 인 경우 Import-Csv를 사용하여 개체를 만드는 것이 좋습니다. 당신이 필요로하는 무엇이든 잡아 이제

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 line 
당신에게주는 그런

[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 

관련 문제