Solved

Further modification to Powershell script

Posted on 2014-09-19
5
140 Views
Last Modified: 2014-09-19
This is a slight modification which is needed to an existing script.

This is a continuation of the assistance I have been receiving today.

Another source file has the following structure for the PO number.
There is a sample of a PO1 and the PID as well below.

BEG*00*NE*99545**20140915~



PO1**20*CA*19.00**UK*12345678945612*PI*1234567~
PID*F****DESC~


 The basic structure for the PO1 and the PID is repeated in the file..

 Sample Script:

 $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 "(NE\*)|(PID\*)|(PO1\*{2})|(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 "(NE\*)(\d{0,10})"){
                  $poline = "PONumber: $($Matches[2])"
            }
            #Collect PO1 details and following PID Discription
            If($_ -match "(PO1\*{2})(\d{1,})\*(\w{1,})\*(\d{1,}(\.\d{1,})?)\*{2}(\w{1,})\*(\d{1,})"){
                  $i++
                  $total += [double]$Matches[2] * [double]$Matches[4]
                  $Desc = ($Data[([Array]::IndexOf($Data,$_)+1)] -split "\*\*\*\*|~")[1]
                  [String[]]$line += "`r`nItem $i`: UPC: $($Matches[7]), Desc: $Desc, Qty: $($Matches[2]) Price  `$$($Matches[4])"
            }
      }
 }
}

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

 How can I modify any of today`s scripts in order to get the desired output?

 Currently the script I am using is not showing anything for PONumber... it's blank after the word PONumber.
everything else is calculated or shown.

 Here is the output desired.

 PONumber: 1234561111
   Item 1: UPC: 12345678912345, Desc: information here, Qty: 248 Price: $10.00
   Item 2: UPC: 23456789123452, Desc: information here, Qty: 329 Price: $1.00
   Item 3: UPC: 98765432198765, Desc: information here, Qty: 20 Price $2.00
   Total: $Calculated total

  PONumber: 1234568888
   Item 1: UPC: 12345678912345, Desc: information here, Qty: 250 Price: $10.00
   Item 2: UPC: 23456789123452, Desc: information here, Qty: 200 Price: $1.00
   Item 3: UPC: 98765432198765, Desc: information here, Qty: 100 Price $2.00
   Total: $Calculated total

What modification to the script needs to be made?
0
Comment
Question by:100questions
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40332564
BEG*00*NE*99545**20140915~
what is the PO number in this line?
0
 

Author Comment

by:100questions
ID: 40332588
The PO number is 99545 in this case.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40332637
'20140915' is this a date format?
0
 

Author Comment

by:100questions
ID: 40332652
Yes, that's the date format.
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40332696
Try..
 $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\*)|(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 "BEG\*" -and $_ -match "\*(\w{2}\*)(\d{3,10})"){
                   $poline = "PONumber: $($Matches[2])"
             }
             #Collect  G80 details and following G81 Description
             If($_ -match "(PO1\*{1,})(\d{1,})\*(\w{1,})\*(\d{1,}(\.\d{1,})?)\*{1,}(\w{1,}|\d{1,})\*{1,}(\d{1,})"){
                   $i++
                   $total += [double]$Matches[2] * [double]$Matches[4]
                   $Desc = ($Data[([Array]::IndexOf($Data,$_)+1)] -split "\*{1,}|~")[2]
                   [String[]]$line += "`r`nItem $i`: UPC: $($Matches[7]), Desc: $Desc, Qty: $($Matches[2]) Price  `$$($Matches[4])"
             }
       }
  }
 }

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

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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