How can I change this Powershell script to list all Purchase Orders?

Here is the source data, in a file named source.txt

ISA*00*          *00*          *01*123456123     *AA*AAAAAAAA       *123456*1234*A*00123*123456789*1*B*>~
GS*AB*123456123*ABCDEFGH*20150526*1234*123456789*A*1234561AAA~
ST*999*123456789~
G50*N*20150505*0888888~
G62*02*20150610~
NTE**MESSAGE~
G66*AB*A****01*SOMEWHERE HERE    ZZ~
N1*BT*COMPANY INC. - CITY*1*1234567891234~
N3*INFO*P. O. BOX 1234~
N4*CITY*ST*123456789~
N1*ST*INFO2*1*1234567891234~
N3*ADDRESS.~
N4*CITY*ST*12345~
N1*AB*COMPANY*1*1234567894563~
G68*1400*CA*10.0000*001234567894~
G69*DESCRIPTION~
G70*10*10.000*OZ~
G68*1500*CA*11.0000*001234567895~
G69*DESCRIPTION2~
G70*10*11.000*OZ~
G76*1600*CA*1000.00*KG*1234.123*AA**1234567~
SE*20*001234567~
ST*999*0012345644~
G50*N*20150505*0999999~
G62*02*20150610~
NTE**MESSAGE~
G66*AB*A****01*SOMEWHERE HERE    ZZ~
N1*BT*COMPANY INC. - CITY*1*1234567891234~
N3*INFO*P. O. BOX 1234~
N4*CITY*ST*123456789~
N1*ST*INFO2*1*1234567891234~
N3*ADDRESS.~
N4*CITY*ST*12345~
N1*AB*COMPANY*1*1234567894563~
G68*1400*CA*12.0000*001234567891~
G69*DESCRIPTION~
G70*10*10.000*OZ~
G68*1500*CA*13.0000*001234567892~
G69*DESCRIPTION2~
G70*10*11.000*OZ~
G76*1600*CA*1000.00*KG*1234.123*AA**1234567~
SE*20*001234567~
GE*2*1111111111~
IEA*1*111111112~
~

Here is the PowerShell script:

$InputFile = "C:\source.txt"
$OutputFile = "C:\sourcenew.txt"

Function ParseText ($OutputFile,$InputFile){
Begin{
             $line,$poline,$shipto,$totline,$total,$i,$n3,$n4 = $null,$null,$null,$null,$null,0,0,0
             Set-Content $OutputFile $null
             $Data = Get-Content -Path $InputFile | ?{$_ -match "(G50\*)|(G69\*)|(G68\*)|(N\d\*)|(IEA\*)"}
       }
 Process{
       $Data | % {
       #Check the end of data set
             If ($_ -match "IEA\*"){
                   If($poline -ne $null -and $total -ne $null){
                         $Totline = "Total: `$$Total"
                         Write-host "This is Total `$$total"
                         #Write the data collection to output file
                         "$poline$shipto$line`r`n$totline`r`n" | out-file $OutputFile -Encoding UTF8 -Append
                         #reset the variables
                         $line,$poline,$shipto,$totline,$total,$i,$n3,$n4 = $null,$null,$null,$null,$null,0,0,0
                   }
             }
             #Collect the PO Number
             If($_ -match "G50\*" -and $_ -match "(\w{2}\*)(\d{3,10})"){
                   $poline = "PO Number: $($Matches[2])"
             }
             #Collect the Ship To information
             If($_ -match "(N3\*)([^*~]+)"){
                   $n3++
                   $n4 = 0 # restart N4 counter after finding N3
                   If($n3 -eq 2){
                         $shipto = "`r`nShip to: $($Matches[2])"
                   }
             }
             If($n3 -eq 2 -and $shipto -ne $null -and $_ -match "(N4\*)([^~]+)"){
                   $n4++
                   If($n4 -eq 1){ # only the first N4 under a N3
                   $CityStateZip = ($Matches[2]).split("*") -join ", "
                   $shipto = "$shipto, $CityStateZip"
                   }
             }               
             #Collect  G68 details and following G69 Description
             If($_ -match "(G68\*{1,})(\d{1,})\*(\w{1,})\*(\d{1,}(\.\d{1,})?)\*{1,}(\w{1,}|\d{1,})"){
                   $i++
                   $total += [double]$Matches[2] * [double]$Matches[4]
                   $Desc = ($Data[([Array]::IndexOf($Data,$_)+1)] -split "\*{1,}|~")[1]
                   [String[]]$line += "`r`nItem $i`: UPC: $($Matches[6]), Desc: $Desc, Qty: $($Matches[2]), Price: `$$($Matches[4])"
             }
       }
  }
  }
 If(Test-Path $InputFile){
  ParseText $OutputFile $InputFile
 }
 

Open in new window


The output currently looks like this, in a file called sourcenew.txt

PO Number: 0999999
Ship to: ADDRESS., CITY, ST, 12345
Item 1: UPC: 001234567894, Desc: DESCRIPTION, Qty: 1400, Price: $10.0000
Item 2: UPC: 001234567895, Desc: DESCRIPTION2, Qty: 1500, Price: $11.0000
Item 3: UPC: 001234567891, Desc: DESCRIPTION, Qty: 1400, Price: $12.0000
Item 4: UPC: 001234567892, Desc: DESCRIPTION2, Qty: 1500, Price: $13.0000
Total: $66800

I would like to modify the script so that it loops through the original source.txt file but also lists the same information however for PO 888888.

The order of the data should be in the order that it finds the data in the source file..

For instance, to list data for PO 888888, blank line, then list data for PO 999999.
100questionsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
The script is already prepared for more than 1 order but is checking for an IEA segment to determine when to 'restart' processing. That segment doesn't occur (in this particular source document) until the very end so try changing IEA to SE (which occurs at the end of each order) on lines 8 and 13:
$InputFile = "C:\source.txt"
$OutputFile = "C:\sourcenew.txt"

Function ParseText ($OutputFile,$InputFile){
Begin{
             $line,$poline,$shipto,$totline,$total,$i,$n3,$n4 = $null,$null,$null,$null,$null,0,0,0
             Set-Content $OutputFile $null
             $Data = Get-Content -Path $InputFile | ?{$_ -match "(G50\*)|(G69\*)|(G68\*)|(N\d\*)|(SE\*)"}
       }
 Process{
       $Data | % {
       #Check the end of data set
             If ($_ -match "SE\*"){
                   If($poline -ne $null -and $total -ne $null){
                         $Totline = "Total: `$$Total"
                         Write-host "This is Total `$$total"
                         #Write the data collection to output file
                         "$poline$shipto$line`r`n$totline`r`n" | out-file $OutputFile -Encoding UTF8 -Append
                         #reset the variables
                         $line,$poline,$shipto,$totline,$total,$i,$n3,$n4 = $null,$null,$null,$null,$null,0,0,0
                   }
             }
             #Collect the PO Number
             If($_ -match "G50\*" -and $_ -match "(\w{2}\*)(\d{3,10})"){
                   $poline = "PO Number: $($Matches[2])"
             }
             #Collect the Ship To information
             If($_ -match "(N3\*)([^*~]+)"){
                   $n3++
                   $n4 = 0 # restart N4 counter after finding N3
                   If($n3 -eq 2){
                         $shipto = "`r`nShip to: $($Matches[2])"
                   }
             }
             If($n3 -eq 2 -and $shipto -ne $null -and $_ -match "(N4\*)([^~]+)"){
                   $n4++
                   If($n4 -eq 1){ # only the first N4 under a N3
                   $CityStateZip = ($Matches[2]).split("*") -join ", "
                   $shipto = "$shipto, $CityStateZip"
                   }
             }               
             #Collect  G68 details and following G69 Description
             If($_ -match "(G68\*{1,})(\d{1,})\*(\w{1,})\*(\d{1,}(\.\d{1,})?)\*{1,}(\w{1,}|\d{1,})"){
                   $i++
                   $total += [double]$Matches[2] * [double]$Matches[4]
                   $Desc = ($Data[([Array]::IndexOf($Data,$_)+1)] -split "\*{1,}|~")[1]
                   [String[]]$line += "`r`nItem $i`: UPC: $($Matches[6]), Desc: $Desc, Qty: $($Matches[2]), Price: `$$($Matches[4])"
             }
       }
  }
  }
 If(Test-Path $InputFile){
  ParseText $OutputFile $InputFile
 }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.