0

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
6
  • Are $file (the input array) and $File (the output string) supposed to be distinct variables? PowerShell is case-insensitive. Commented May 9, 2017 at 19:44
  • If you need a fixed format for the output file, I strongly recommend that you explore the -f operator Commented May 9, 2017 at 19:49
  • Also, you're only setting $Test once outside the loop. Because its value never changes inside the loop, the if() takes the same branch every time. Commented May 9, 2017 at 19:49
  • Good catch @Ryan Bemrose, just fixed the variables names. Humm, will take a look at the $Test you mentioned, thank you. Commented May 9, 2017 at 20:09
  • You can't have D:\Test as an input file and at the same time a directory D:\Test when creating the output file. Commented May 9, 2017 at 20:17

1 Answer 1

1
  • your substring offsets don't match your sample data
  • to check the offsets you should (at least) temporarily insert a ruler
  • to fill a defined length string with spaces simply do $spaces = " " * 21
  • but as you were told better use a -f format string for output, see script.
  • instead of fiddling with two values and inserting a dot, simply get the whole range and divide by 100 (leading zeroes are ignored)

$InputFile = 'D:\Test\inputfile.txt'
$outFile =   'D:\Test\outfile.txt'
$Pattern = "010TESTS"

Get-Content $InputFile | Select-String -Pattern $Pattern | ForEach-Object {
    $Line = [string]$_
    $Line.Substring(70, 8)  #this should give me the value 010TESTS
    $Value1 = $Line.Substring(57, 8)
    $Value2 = [double]$Line.Substring(137, 8) / 100

    if ( $Value1 -and $Value2 ) {
      "{0,25}{1,30:N2}" -f $Value1,$Value2 | Out-File $OutFile -Append
    }
}
  • $_ contains the line currently processed by the Forach-Object
  • [string] casts (converts) to a string, which is a requirement for .substring
  • the first number in curly brackets represents the order in which the vars follow the -f
  • the second number in curly brackets following the comma denotes the places the var will seize (negative numbers mean left justified, positive right justified)
  • following the colon is a format specifier (see Jeff Zeitlin's link) N2 means a floating point number with 2 decimal places.

This sample output has due to my locale a decimal comma. (ruler added manually)

         1         2         3         4         5     
1234567890123456789012345678901234567890123456789012345
                 CODE123                          14,83
                 CODE123                           3,36
                 CODE123                           1,20
                 CODE123                          41,24
                 CODE123                           1,02
Sign up to request clarification or add additional context in comments.

3 Comments

LotPings thank you so much for the valuable tips, I don´t know the -f formatting very well but will have to study, so what does it mean exactly in this code? "{0,25}{1,30:N2}" Also I did not understand this part, what does it do? $Line = [string]$_
Added some comments to your questions.
Perfect! It all makes sense now, thanks a lot again for the explanation, helped a bunch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.