Solved

Modification to current Powershell script to produce desired output.

Posted on 2014-09-18
1
141 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

16 Experts available now in Live!

Get 1:1 Help Now