Solved

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

Posted on 2014-10-21
5
87 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

634 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