Solved

How can I modify this Powershell script to display the Bill to information?

Posted on 2014-10-20
4
70 Views
Last Modified: 2014-10-21
Current Script:

$InputFile = "C:\Data\Original.txt"
$OutputFile = "C:\Data\New.txt"

Function ParseText ($OutputFile,$InputFile){
Begin{
            $line,$poline,$shipto,$totline,$total,$i,$n3 = $null,$null,$null,$null,$null,0,0
            Set-Content $OutputFile $null
            $Data = Get-Content -Path $InputFile | ?{$_ -match "(SA\*)|(PID\*)|(PO1\*{2})|(N\d\*)|(IEA\*)"}
      }
Process{
      $Data | % {
      #Check the end of data set
            If ($_ -match "IEA\*"){
                  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$shipto$line`r`n$totline`r`n" | out-file $OutputFile -Encoding UTF8 -Append
                        #reset the variables
                        $line,$poline,$shipto,$totline,$total,$i,$n3 = $null,$null,$null,$null,$null,0,0
                  }
            }
            #Collect the PO Number
            If($_ -match "(SA\*)(\d{0,10})"){
                  $poline = "PO Number: $($Matches[2])"
            }
            #Collect the Ship To information
            If($_ -match "(N3\*)([^*~]+)"){
                  $n3++
                  If($n3 -eq 2){
                        $shipto = "`r`nShip to: $($Matches[2])"
                  }
            }
            If($n3 -eq 2 -and $shipto -ne $null -and $_ -match "(N4\*)([^*~]+)"){
                  $shipto = "$shipto, $($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
}

Open in new window


In the output this code produces, I would like the detail from the first N3 and N4 segments it finds, and not the 2nd instance of the N3 and N4 segments.

Is this possible?
0
Comment
Question by:100questions
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40392924
Have you tried changing the '2' in both places where it is checked, so change "$n3 -eq 2" to "$n3 -eq 1" on lines 30 and 34?
0
 

Author Comment

by:100questions
ID: 40392928
Let me try and will let you know, thanks again.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40392992
Or maybe you want both lines, if present? Then you could do something like this:
Function ParseText ($OutputFile,$InputFile){
Begin{
            $line,$poline,$billto,$shipto,$totline,$total,$i,$n3 = $null,$null,$null,$null,$null,$null,0,0
            Set-Content $OutputFile $null
            $Data = Get-Content -Path $InputFile | ?{$_ -match "(SA\*)|(PID\*)|(PO1\*{2})|(N\d\*)|(IEA\*)"}
      }
Process{
      $Data | % {
      #Check the end of data set
            If ($_ -match "IEA\*"){
                  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$billto$shipto$line`r`n$totline`r`n" | out-file $OutputFile -Encoding UTF8 -Append
                        #reset the variables
                        $line,$poline,$billto,$shipto,$totline,$total,$i,$n3 = $null,$null,$null,$null,$null,$null,0,0
                  }
            }
            #Collect the PO Number
            If($_ -match "(SA\*)(\d{0,10})"){
                  $poline = "PO Number: $($Matches[2])"
            }
            #Collect the Bill/Ship To information
            If($_ -match "(N3\*)([^*~]+)"){
                  $n3++
                  If($n3 -eq 1){
                        $billto = "`r`nBill to: $($Matches[2])"
                  }
                  If($n3 -eq 2){
                        $shipto = "`r`nShip to: $($Matches[2])"
                  }
            }
            If($_ -match "(N4\*)([^*~]+)"){
                  If($n3 -eq 1 -and $billto -ne $null){
                        $billto = "$billto, $($Matches[2])"
                  }
                  If($n3 -eq 2 -and $shipto -ne $null){
                        $shipto = "$shipto, $($Matches[2])"
                  }
            }
            #Collect PO1 details and following PID Description
            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])"
            }
      }
 }
}

Open in new window

0
 

Author Closing Comment

by:100questions
ID: 40394362
Excellent, thanks again.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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 …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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