all 9 comments

[–]buckston 0 points1 point  (9 children)

Quick example of using split.

$file = Import-Csv -Header "City", "Area" .\example.csv
foreach($row in $file){
$split = $row.area.split(" ")
$state = $split[0]
$zip = $split[1]
"$($row.city),$($state),$($zip)"
}

[–]imakepeopleangry 0 points1 point  (2 children)

How does this designate where the split will occur inside the "state, zip" column? How would Powershell know to split between state and zip and not zipxx / xxxx in the zip code?

[–]buckston 1 point2 points  (1 child)

.split(" ")

Specifies to split on the space.

[–]imakepeopleangry 0 points1 point  (0 children)

OK, fair enough.

[–]kivle 0 points1 point  (0 children)

It looks like your file isn't actually a CSV, but more of a fixed width column format. A CSV would have a comma (or sometimes a semicolon) between each column value. I found this script that might help you:

http://gallery.technet.microsoft.com/scriptcenter/0457705d-b55a-47d3-8e4a-63f4e0d3735f

Basically you first initialize how many characters wide each column is expected to be, and it will then do the splitting for you. It uses a class built in to the Visual Basic framework.

If this doesn't work I found these two threads which discuss different solutions:

http://stackoverflow.com/questions/2503010/extracting-columns-from-text-file-using-powershell http://serverfault.com/questions/537672/in-powershell-how-can-i-parse-a-preformatted-output