Solved

How can I modify the Powershell script in two more scenarios

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article will help you understand what HashTables are and how to use them in PowerShell.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

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

17 Experts available now in Live!

Get 1:1 Help Now