How can I modify this Powershell script to only look for the first N3 and N4 segment?

I would like to modify the following script:

$InputFile = "C:\Data\Original.txt"
$OutputFile = "C:\Data\New.txt"

 Function ParseText ($OutputFile,$InputFile){
 Begin{
             $line,$poline,$shipto,$totline,$total,$i,$n3 = $null,$null,$null,$null,$null,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 = $null,$null,$null,$null,$null,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++
                   If($n3 -eq 2){
                         $shipto = "`r`nShip to: $($Matches[2])"
                   }
             }
             If($n3 -eq 2 -and $shipto -ne $null -and $_ -match "(N4\*)([^*~]+)"){
                   $shipto = "$shipto, $($Matches[2])"
             }               
             #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


My focus is on the N3 and N4 segment.
At the moment, with the script as is, will look at the 2nd instance it finds of the N3 and N4, which is good.
However, if there is a 3rd instance of N4, it will also show that as well.  I don't want the script to look for a 3rd instance of a line starting with N4.  If it finds a 3rd instance of a line that starts with N4, it must not do anything with it.
100questionsAsked:
Who is Participating?
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:
Do you have an example document where this happens? I have constructed a document where the problem happens and solved it with the code below but I felt unsure if I could reproduce the problem with a valid document. An N4 can normally only be used right after an N3, right? If not then the check must be changed, actually it would be better anyway to check for N1 records and the type (BT/ST etc) instead of counting N3 records. I must admit though that my ANSI X12 knowledge is a bit rusty...

Anyway, here is the code:
 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
                         $shipto = "$shipto, $($Matches[2])"
                   }
             }               
             #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

tested by putting this in the document:
  N1*SU*Company*8*123456789~
  N1*BT*Name*8*1234567891234~
  N3*1 Company~
  N4*LOC*BB*ABC  DEF*CA~
  N1*ST*D021 Loc*9*1234567891236~
  N3*Street~
  N4*Location*BB*ABC DEF*CA~
  N1*XX*D021 Loc2*9*1234567891236~
  N4*Location2*BB*ABC DEF*CA~

Open in new window

old output:
PO Number: 8888888887
Ship to: Street, Location, Location2
Item 1: UPC: UK, Desc: 1, Qty: 10, Price: $12
Total: $120

PO Number: 8888888891
Item 1: UPC: UK, Desc: 1, Qty: 2, Price: $40.10
Total: $80.2

Open in new window

new output:
PO Number: 8888888887
Ship to: Street, Location
Item 1: UPC: UK, Desc: 1, Qty: 10, Price: $12
Total: $120

PO Number: 8888888891
Item 1: UPC: UK, Desc: 1, Qty: 2, Price: $40.10
Total: $80.2

Open in new window

I'm not sure if the rest of the output is ok because I only had an old input document (this code uses G50/G68 instead of SA/PO1) but the code change itself should be usable in either version.

By the way, regarding that remark I made earlier about checking N1 records instead of counting N3 records: I was testing a bit of code but haven't made it into a working script yet. This could be changed to account for optional/multiple N2/3/4 records. It goes something like this:
             #Collect the Ship To information
             If($_ -match "(N1\*)(\w+)(\*)"){
                   $ntype = $Matches[2]
             }
             If($_ -match "(N3\*)([^*~]+)"){
                   If($ntype -eq "ST"){
                         $shipto = "`r`nShip to: $($Matches[2])"
                   }
             }
             If($_ -match "(N4\*)([^*~]+)"){
                   If($ntype -eq "ST" -and $shipto -ne $null){
                         $shipto = "$shipto, $($Matches[2])"
                   }
             }               

Open in new window

0

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
100questionsAuthor Commented:
Excellent work, thanks.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.