Solved

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

Posted on 2014-10-21
5
82 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

23 Experts available now in Live!

Get 1:1 Help Now