Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Further modification to Powershell script

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?
Avatar of SubSun
SubSun
Flag of India image

BEG*00*NE*99545**20140915~
what is the PO number in this line?
Avatar of E=mc2

ASKER

The PO number is 99545 in this case.
'20140915' is this a date format?
Avatar of E=mc2

ASKER

Yes, that's the date format.
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial