Solved

Modification to current Powershell script to produce desired output.

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

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…
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 …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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