Solved

Modification to current Powershell script to produce desired output.

Posted on 2014-09-18
1
142 Views
Last Modified: 2014-09-19
This is a continuation of the assistance I have been receiving today.

Another source file has the following structure in the PO1 and PID.

PO1**10*CA*10.61**UA*123456789456*IN*14524578*VN*45678~
CTP*ST*PQR*11.00~
PID*F*08***DESCRIPTION~

This basic structure 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 "(SA\*)|(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 "(SA\*)(\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 the description, 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
0
Comment
Question by:100questions
1 Comment
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40331451
Change line
$Desc = ($Data[([Array]::IndexOf($Data,$_)+1)] -split "\*\*\*\*|~")[1]

Open in new window

to
$Desc = ($Data[([Array]::IndexOf($Data,$_)+1)] -split "\*\d{1,}|\*{1,}|~" | ?{$_})[2]

Open in new window

0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article will help you understand what HashTables are and how to use them in PowerShell.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

23 Experts available now in Live!

Get 1:1 Help Now