Reputation: 107
I´m stuck in this scenario. I have a text file that has many lines like this:
123450,ADN,,2785,"1,576,000,000.06",TEXT TEST TEXT,,
999999,NSU,,1234,"-1,576,000,000.06",TEXT TEST TEXT TEST,,
790834,CHI,,5678,"2,345,000,000.01","TEXT TEST (TEXT), TEST",,
893472,JAP,,0123,"-2,345,000,000.01","TILL THERE (ALMOST), UH",,
093289,CRU,,6489,"424,000,000.00",TEST TEXT UB,,
And I need to do formatting to see the results like this:
then the value without ,
, .
, and "
(18 characters fixed width)
and the text without ,
, .
, and "
:
00000000123450ADN000002785+000000157600000006TEXT TEST TEXT
00000000230634NSU000001234+000000157600000006TEXT TEST TEXT TEST
00000000232014CHI000005678+000000234500000001TEXT TEST (TEXT) TEST
00000000230634JAP000000123+000000234500000001TILL THERE (ALMOST) UH
00000000232015CRU000006489+000000042400000000TEST TEXT UB
I am trying to do it using Format-Table
, but I am not able to manipulate and specify the leading zeroes and fixed width. How can I do it?
Upvotes: 1
Views: 270
Reputation: 9266
You format it the same way you build any complex formatted string: One field at a time. PowerShell can use .NET string format specifiers with the -f
operator.
Standard format specifiers such as {N:DX}
can format the number fields (once they're converted to [int]
). We first do some string handling on the text fields.
$rawInput = @"
123450,ADN,,2785,"1,576,000,000.06",TEXT TEST TEXT,,
999999,NSU,,1234,"-1,576,000,000.06",TEXT TEST TEXT TEST,,
790834,CHI,,5678,"2,345,000,000.01","TEXT TEST (TEXT), TEST",,
893472,JAP,,0123,"-2,345,000,000.01","TILL THERE (ALMOST), UH",,
093289,CRU,,6489,"424,000,000.00",TEST TEXT UB,,
"@
$items = ConvertFrom-Csv $rawInput -Header 'num1','alpha1','blank1','num2','num3string','text','blank2','blank3'
$items | foreach {
# Remove unwanted characters from num3string field and pad left with zeroes
# Note this also discards the `-` character, which was not specified, but it is
# necessary to get the desired output.
$numString = ($_.num3string -replace '\.|\,|\-','').PadLeft(18, '0')
# Strip unwanted characters from the text field as well
$textField = $_.text -replace '\.|\,|\"',''
# Build the string using .NET-style format strings.
# To leading-pad the numbers, use {N:DX} which means
# format argument N as a decimal int X chars wide
'{0:D14}{1}{2:D9},{3}{4}' -f (
[int]$_.num1,
$_.alpha1,
[int]$_.num2,
$numString,
$textField
)
} | Out-File 'out.txt' # Send output to a file
You don't specify in your question how you are reading the data into PowerShell. Obviously, if you've already imported the CSV into a PowerShell variable, you should use your own fields.
Upvotes: 1
Reputation: 2312
I would try something like this (replace the file paths with your own):
$infile = Import-Csv "C:\Temp\input.txt" -Delimiter "," -Header 1,2,3,4,5,6,7,8
$outfile = "C:\Temp\output.txt"
if(Test-Path $outfile){ Remove-Item $outfile } # Remove any existing output files
foreach($line in $infile) # Loop through lines
{
$outline = "" # Empty output line
$outline =
($line.1).PadLeft(14,"0") +
$line.2 +
($line.4).PadLeft(12,"0") +
"+" +
($line.5).Replace(",","").Replace(".","").Replace("-","").PadLeft(18,"0") +
($line.6).Replace(",","").Replace(".","")
$outline | Out-File $outfile -Append # Write to output file
}
The key to the output spacing is to use padleft and replace the characters you don't want.
Upvotes: 1