Solved

How can I modify the Powershell script in two more scenarios

Posted on 2014-09-19
5
184 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
A brief introduction to what I consider to be the best editor for PowerShell.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

23 Experts available now in Live!

Get 1:1 Help Now