Solved

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

Posted on 2014-09-18
3
162 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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