Solved

How can I change this script to output various parts of the N4 segment?

Posted on 2014-10-21
5
76 Views
Last Modified: 2014-10-22
I am working with this 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 1){
                        $shipto = "`r`nShip to: $($Matches[2])"
                  }
            }  
            If($n3 -eq 1 -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,})?).*\*(\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


Currently in the script output, the N3 and N4 segment looks something like this..

Ship to: Street location, City*State*12345
(The 12345 is the Zip code)

What I would like to output for the N3 and N4 segment is:
Ship to: Street location, City, State, 12345

The original N3 and N3 segments look like this in the input data:

N3*Street location~
N4*CITY*STATE*12345~

What changes do I need to make to the script?
0
Comment
Question by:100questions
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
Phil Bossman earned 500 total points
ID: 40396105
split the N4 from with the * and join with ", "
Lines: 36 - 37

$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 1){
                        $shipto = "`r`nShip to: $($Matches[2])"
                  }
            }  
            If($n3 -eq 1 -and $shipto -ne $null -and $_ -match "(N4\*)([^~]+)"){
                  $n4 = ($Matches[2]).split("*") -join ", "
                  $shipto = "$shipto, $n4"
            }  
            #Collect PO1 details and following PID Discription
            If($_ -match "(PO1\*{2})(\d{1,})\*(\w{1,})\*(\d{1,}(\.\d{1,})?).*\*(\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

0
 

Author Closing Comment

by:100questions
ID: 40397074
Works excellent.  Thanks.
0
 

Author Comment

by:100questions
ID: 40397152
And if I wanted to change this portion of another script so that I have the same effect, should I open up another request?

     }
             #Collect the Ship To information
             If($_ -match "(N3\*)([^*~]+)"){
                   $n3++
                   $n4 = 0 # restart N4 counter after finding N3
                   If($n3 -eq 2){
                         $shipto = "`r`nShip to: $($Matches[2])"
                   }
             }
             If($n3 -eq 2 -and $shipto -ne $null -and $_ -match "(N4\*)([^~]+)"){
                   $n4++
                   If($n4 -eq 1){ # only the first N4 under a N3
                         $shipto = "$shipto, $($Matches[2])"
                   }
             } 

Open in new window

0
 
LVL 3

Expert Comment

by:Phil Bossman
ID: 40397731
Replace line 13  with

                  $CityStateZip = ($Matches[2]).split("*") -join ", "
                  $shipto = "$shipto, $CityStateZip"

Open in new window

0
 

Author Comment

by:100questions
ID: 40397977
Thanks, works very well.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
A procedure for exporting installed hotfix details of remote computers using powershell
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the fileā€¦

746 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

12 Experts available now in Live!

Get 1:1 Help Now