Fixed width text.txt

by lemonade at 2013-02-06 02:46:52

I have done simple get-content | foreach-object > output scripts before. But struggling to get the output i need.

Trying to take a difficult fixed width text file and turn into comma separated text file.

Could do with text in the attached text file text.txt, outputting like text.txt. Is this possible?

Thanks
David
by DonJ at 2013-02-06 13:30:43
Well, you don’t really tell me what you want as opposed to what you got, and you didn’t show me any code. Little tough to help.

In general, I don’t do these as one-liners. A script is far easier.


$lines = Get-Content input.txt
foreach ($line in $lines) {
$fields = @()
$fields[0] = $line.SubString(0,8).Trim()
$fields[1] = $line.Substring(9,10).Trim()
$fields -join ‘,’ | Out-File output.txt -append
}


For an input file where the fields are all of a known position and length, this is pretty straightforward.