Solved

How can I get this Powershell script to produce multiple output?

Posted on 2014-09-18
3
161 Views
Last Modified: 2014-09-18
Source Data:

AIS*00*          *00*          *11*123456CORP     *01*1234567891     *00*123456*R*13345*123456789*0*A*>~
GS*PO*9999999998*4165551010*20140918*0123*200*Y*001000~
ST*850*235454~
BEG*00*SA*12345678**20140918~
PER*BD*NAME*PQ*1234567894~
FOB*PC*OR*                  55~
DTM*002*20141009~
TD5****M~
N9*L1**MESSAGE~
MSG*MESSAGE~
MSG*MESSAGE~
N1*AB*ALPHA DATA*0*1234564567898~
N3*STREET~
N4*LOC*BA*12345~
N1*VN*PRODUCT DESC*8*1234567894565~
PO1**10*CA*9.80*FX*UA*12345645454*PI*2134567~
PID*F****DESC 1~
PO4*10*10*OZ~
PO1**18*CA*21.72*FX*UA*456123789484*PI*987654~
PID*F****DESC 2~
PO4*12*8*OZ~
PO1**9*CA*40*FX*UA*123456789456*PI*7894561~
PID*F****DESC 3~
PO4*12*16*OZ~
PO1**76*CA*21*FX*UA*123849745687*PI*2345678~
PID*F****DESC 4~
PO4*12*10*OZ~
PO1**100*CA*7.08*FX*UA*123456789787*PI*1234567~
PID*F****DESC~
CTT*5**9999*LB*50*CF~
SE*40*123456~
GE*1*130~
EFG*1*000000100~
AIS*00*          *00*          *11*123456CORP     *01*1234567891     *00*123456*R*13345*123456789*0*A*>~
GS*PO*9999999788*4165551010*20140918*0123*200*Y*001000~
ST*850*235454~
BEG*00*SA*12344444**20140918~
PER*BD*NAME*PQ*1234567894~
FOB*PC*OR*                  55~
DTM*002*20141009~
TD5****M~
N9*L1**MESSAGE~
MSG*MESSAGE~
MSG*MESSAGE~
N1*AB*ALPHA DATA*0*1234564567898~
N3*STREET~
N4*LOC*BA*12345~
N1*VN*PRODUCT DESC*8*1234567894565~
PO1**11*CA*9.80*FX*UA*12345645454*PI*2134567~
PID*F****DESC 1~
PO4*10*10*OZ~
PO1**11*CA*21.72*FX*UA*456123789484*PI*987654~
PID*F****DESC 2~
PO4*12*8*OZ~
PO1**9*CA*40*FX*UA*123456789456*PI*7894561~
PID*F****DESC 3~
PO4*12*16*OZ~
PO1**75*CA*21*FX*UA*123849745687*PI*2345678~
PID*F****DESC 4~
PO4*12*10*OZ~
PO1**100*CA*7.08*FX*UA*123456789787*PI*1234567~
PID*F****DESC~
CTT*5**9999*LB*50*CF~
SE*40*123456~
GE*1*130~
EFG*1*000000100~



Script to work with:

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

Function ParseText ($OutputFile,$InputFile){
Begin{
            $line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
            Set-Content $OutputFile $null
            $Data = Get-Content -Path $InputFile | ?{$_ -match "(SA\*)|(PID\*)|(PO1\*{2})|(IEA\*)"}
      }
Process{
      $Data | % {
      #Check the end of data set
            If ($_ -match "EFG\*"){
                  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$line`r`n$totline`r`n" | out-file $OutputFile -Encoding UTF8 -Append
                        #reset the variables
                        $line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
                  }
            }
            #Collect the PO Number
            If($_ -match "(SA\*)(\d{0,10})"){
                  $poline = "PONumber: $($Matches[2])"
            }
            #Collect PO1 details and following PID Discription
            If($_ -match "(PO1\*{2})(\d{1,})\*(\w{1,})\*(\d{1,}(\.\d{1,})?)\*{2}(\w{1,})\*(\d{1,})"){
                  $i++
                  $total += [double]$Matches[2] * [double]$Matches[4]
                  $Desc = ($Data[([Array]::IndexOf($Data,$_)+1)] -split "\*\*\*\*|~")[1]
                  [String[]]$line += "`r`nItem $i`: UPC: $($Matches[7]), Desc: $Desc, Qty: $($Matches[2]) Price  `$$($Matches[4])"
            }
      }
 }
}

If(Test-Path $InputFile){
 ParseText $OutputFile $InputFile
}


Sample of Output desired, not reflective of actual numbers in data above.

PONumber: 1234561234
 Item 1: UPC: 12345678912345, Desc: information here, Qty: 248 Price: $10.00
 Item 2: UPC: 23456789123452, Desc: information here, Qty: 329 Price: $1.00
 Item 3: UPC: 98765432198765, Desc: information here, Qty: 20 Price $2.00
 Total: $Calculated total

PONumber: 1234561235
 Item 1: UPC: 12345678912345, Desc: information here, Qty: 250 Price: $10.00
 Item 2: UPC: 23456789123452, Desc: information here, Qty: 200 Price: $1.00
 Item 3: UPC: 98765432198765, Desc: information here, Qty: 100 Price $2.00
 Total: $Calculated total


Currently, an output file is being created however it is blank.
0
Comment
Question by:100questions
  • 2
3 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40331319
What changed in input file? compared to your previous question.
0
 

Author Comment

by:100questions
ID: 40331334
The input file has different data, and for some reason it does not produce the output desired at all, it`s blank..
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40331343
OK I could see the PO1 line format has changed a little and the in line 8 pattern was incorrect, in actual code, end of the pattern was (IEA\*) instead of (EFG\*). I have made the changes in PO1 line matching pattern (line 28) . Check and see if it works..
$InputFile = "C:\Original.txt"
$OutputFile = "C:\New.txt"

Function ParseText ($OutputFile,$InputFile){
Begin{
            $line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
            Set-Content $OutputFile $null
            $Data = Get-Content -Path $InputFile | ?{$_ -match "(SA\*)|(PID\*)|(PO1\*{2})|(EFG\*)"}
      }
Process{
      $Data | % {
      #Check the end of data set
            If ($_ -match "EFG\*"){
		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$line`r`n$totline`r`n" | out-file $OutputFile -Encoding UTF8 -Append
                        #reset the variables
                        $line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
                  }
            }
            #Collect the PO Number
            If($_ -match "(SA\*)(\d{0,10})"){
                  $poline = "PONumber: $($Matches[2])"
            }
            #Collect PO1 details and following PID Discription
            If($_ -match "(PO1\*{2})(\d{1,})\*(\w{1,})\*(\d{1,}(\.\d{1,})?).*\*(\w{1,})\*(\d{1,})"){
                  $i++
                  $total += [double]$Matches[2] * [double]$Matches[4]
                  $Desc = ($Data[([Array]::IndexOf($Data,$_)+1)] -split "\*\*\*\*|~")[1]
                  [String[]]$line += "`r`nItem $i`: UPC: $($Matches[7]), Desc: $Desc, Qty: $($Matches[2]) Price  `$$($Matches[4])"
            }
      }
 }
}

If(Test-Path $InputFile){
 ParseText $OutputFile $InputFile
}

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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