Solved

How can I modify the Powershell script in two more scenarios

Posted on 2014-09-19
5
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A brief introduction to what I consider to be the best editor for PowerShell.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scannerā€¦
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

710 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