2017-09-25 4 views
0

새로운 헤더를 생성하기 위해 CSV 헤더를 하위 문자열, 그래서 헤더 추가 :내가 헤더가없는 CSV 파일이

$file = "<path to file>" 
$list = Import-Csv $file -Header Unit, IP, Hostname 

결과는 다음과 같이 :

 
Unit    IP   Hostname 
----    --   -------- 
1234 Camden London 192.168.1.1 Server1 

I을 "Camden London"에게 "Unit"열에 "1234"를 유지하면서 자신의 헤더를 제공하고 싶습니다.

 
Unit Town   IP   Hostname 
---- ----   --   -------- 
1234 Camden London 192.168.1.1 Server1 

나는 자신의 목록에 그들을 분리 할 수 ​​있지만 어디 Import-Csv이 인라인 일에 시작하는 모른다 - 할 수있는 사람이 도움이 될까요?

$Unit = $list | Select {$_.Unit.Substring(0,4)} 
$Town = $list | Select {$_.Unit.Substring(5)} 

답변

2

사용 calculated properties은 분할 필드를 만들고 원래 Unit 필드를 제외하려면 :

Import-Csv $file -Header Unit, IP, Hostname | 
    Select-Object -Property @{n='Unit';e={$_.Unit.Substring(0,4)}}, 
     @{n='Town';e={$_.Unit.Substring(5)}}, * -Exclude Unit 
+0

대단히 감사합니다! – JDGEEK

0

을 또는 당신은 객체를 다시 만들 수 있습니다 :

Import-Csv $file -Header Unit, IP, Hostname | %{ 
[pscustomobject]@{ 
    Unit=$_.Unit.Substring(0,4) 
    Town=$_.Unit.Substring(5) 
    IP=$_.IP 
    Hostname=$_.Hostname 
} 

}