Solved

How can I modify the Powershell script in two more scenarios

Posted on 2014-09-19
5
186 Views
Last Modified: 2014-09-23
I would like to modify the Powershell script in these further 2 scenarios.

What changes in the source data is this.

Scenario 1:

BEG*00*SA*1234567**20140917<NL>  
The PO number here is the 1234567

PO1**240*CA***UN*123456789456*VN*12345*IN*456789<NL>
CTP**UCP*23.4<NL>
The amount is 240, the UPC is the 123456789456 and the Price is the 23.40

Scenario 2:
BEG*00*SA*7894561236**20140918<NL>
The PO is 7894561236

PO1**70*CA***UK*12345678912345<NL>
CTP**UCP*19.95<NL>
The amount is 70, the UPC is 12345678912345, and the Price is 19.95

How can I modify the script further to accommodate for each of these scenarios.
0
Comment
Question by:100questions
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40333075
Is price is coming in very next line of PO1 line?
0
 

Author Comment

by:100questions
ID: 40333098
Yes, from the CTP line, in both cases.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40333518
Once more info needed, Do you have a PID line this in this input file? If yes does it comes after CTP line?
0
 

Author Comment

by:100questions
ID: 40333561
Yes, thanks for catching that, I had omitted it.  It comes after the CTP line and it looks like this:

Scenario 1:
PO1**240*CA***UN*123456789456*VN*12345*IN*456789<NL>
CTP**UCP*23.4<NL>
PID*F****DESCRIPTION GOES HERE<NL>


Scenario 2:
PO1**70*CA***UK*12345678912345<NL>
CTP**UCP*19.95<NL>
PID*F*08***DESCRIPTION GOES HERE<NL>
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40333610
Try this code and see if you get expected output..
$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 "(BEG\*)|(PO1\*)|(CTP\*)|(PID\*)|(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{3,10})"){
                   $poline = "PONumber: $($Matches[2])"
             }
             #Collect  G80 details and following G81 Description
             If($_ -match "(PO1\*{1,})(\d{1,})\*\w{2}\*{1,}\w{2}\*(\d{1,})"){
						 $count = $Matches[2]
						 $UPC = $Matches[3]
						 }
						 If($_ -match "(CTP\*{1,}).*\*(\d{1,}(\.\d{1,})?)"){
                   $i++
                   $total += [double]$count * [double]$Matches[2]
                   $Desc = $Data[([Array]::IndexOf($Data,$_)+1)] -Replace "PID\*\w\*{1,}(\d+\*{1,})?|<NL>"
                   [String[]]$line += "`r`nItem $i`: UPC: $UPC, Desc: $Desc, Qty: $count Price  `$$($Matches[2])"
             }
       }
  }
 }

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

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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 …
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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…

830 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