So I have this input file (no file extension):
0 1 2 3 4 5 ###6#### _7______ 8 9 0 1 2 3 ###4##__
012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456
0202XXXX TESTS1 XX DRN CODE123 010TESTS 0000000001481500000014832000000148150000001481700000014832+
0202XXXX TESTS1 TEST11 DRN CODE123 010TESTS 0000000000336400000003364000000033640000000336400000003364+
0202XXXX TESTS1 XXX YYYYYY PN N2 CODE123 010TESTS 0000000000118500000001206000000011600000000118300000001205+
0202XXXX TESTS1 ZZZZZZZZZ DRN CODE123 010TESTS 0000000004188300000041883000000412400000004156200000041240+
0202XXXX TESTS1 PPP MKLRTR PN CODE123 010TESTS 0000000000000000000000000000000000000000000000000000000000/
0202XXXX TESTS1 GGG TEST ON ED CODE123 010TESTS 0000000000095000000001024000000009500000000098700000001024-
And I´d like to get three values from there and print on a new file with a specific formatting.
I´m trying using the substring option, but I am not able to see the results on output file as I expect, so what could be wrong with this code?
I expect to see the output as:
1 2 3 4 5
1234567890123456789012345678901234567890123456789012345
CODE123 17.98
CODE12 2.37
CODE 2.70
CODE12 2.73
CODE123 13.39
But using the below code I´m getting the result without columns. It´s all messed up in one line.
$InputFile = D:\Test\inputfile.txt
$outFile = D:\Test\outfile.txt
$Pattern = "010TESTS"
$spaces1 = " ";
$spaces2 = " ";
$dot = "."
$rec = 0
(gc $InputFile) | ? {$_.trim() -ne "" } | Select-String -pattern $Pattern -AllMatches | Set-Content $outFile
$file = gc $outFile
$Test = $file.Substring(69, 8) # This should give me the value 010TESTS
foreach ($line in $file) {
if ($Test -eq "010TESTS") {
$Value1 = $file.Substring(57, 8)
$Value2 = $file.Substring(137, 6).TrimStart('0') # To remove leading zeroes
$Value3 = $file.Substring(143, 2)
if (! ($Value1 -eq "" -and $Value2 -eq "00")) {
# Here I need the values on each line
$FinalFile += ($spaces1 + $Value1 + $spaces2 + $Value2 + $dot + $Value3).Split("`n")
}
}
}
$FinalFile | Set-Content $outFile
$file(the input array) and$File(the output string) supposed to be distinct variables? PowerShell is case-insensitive.-foperator$Testonce outside the loop. Because its value never changes inside the loop, theif()takes the same branch every time.D:\Testas an input file and at the same time a directoryD:\Testwhen creating the output file.