Solved

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

Posted on 2014-10-20
4
68 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

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

914 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

18 Experts available now in Live!

Get 1:1 Help Now