Solved

Modification to current Powershell script to produce desired output.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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ā€¦

737 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