Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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…
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

708 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

15 Experts available now in Live!

Get 1:1 Help Now