Solved

How can I modify the Powershell script in two more scenarios

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

624 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