Solved

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

Posted on 2014-10-21
2
82 Views
Last Modified: 2014-10-22
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.
0
Comment
Question by:100questions
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40396299
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
 

Author Closing Comment

by:100questions
ID: 40397059
Excellent work, thanks.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Utilizing an array to gracefully append to a list of EmailAddresses
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now