Solved

How can I modify this Powershell script to add the location?

Posted on 2014-10-15
5
95 Views
Last Modified: 2014-10-20
Current script:

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

Function ParseText ($OutputFile,$InputFile){
Begin{
		$line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
		Set-Content $OutputFile $null
		$Data = Get-Content -Path $InputFile | ?{$_ -match "(SA\*)|(PID\*)|(PO1\*{2})|(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$line`r`n$totline`r`n" | out-file $OutputFile -Encoding UTF8 -Append
				#reset the variables
				$line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
			}
		}
		#Collect the PO Number
		If($_ -match "(SA\*)(\d{0,10})"){
			$poline = "PO Number: $($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



Sample Source Data:

  ISA*00*          *00*          *AA*PPPPPP         *11*4165555555     *999999*0001*P*00123*00000020*0*R*>~
  GS*PO*4161234567*4164567894*20100917*0018*0199*X*001111PQRS~
  ST*150*12345678~
  BEG*00*SA*8888888887**20140917~
  FOB*PB~
  DTM*119*20131002~
  DTM*004*20131008~
  N9*AH*4802028592~
  MSG*Message.~
  MSG*Message~
  MSG*Message~
  N1*SU*Company*8*123456789~
  N1*BT*Name*8*1234567891234~
  N3*1 Company~
  N4*LOC*BB*ABC  DEF*CA~
  N1*ST*D021 Loc*9*1234567891236~
  N3*Street~
  N4*Location*BB*ABC DEF*CA~
  PO1**10*CA*12**UK*12345678912345~
  CTP**UCP*12~
  PID*F****ITEM 1 DESCRIPTION~
  PO4*10~
  PO1**50*CA*20**UK*23456789123456~
  PID*F****ITEM 2 DESCRIPTION~
  PO1**30*CA*14.03**UK*34567891234567~
  CTP**UCP*14.03~
  PID*F****ITEM 3 DESCRIPTION~
  PO4*15~
  PO1**10*CA*20.50**UK*45678912345678~
  CTP**UCP*20.50~
  PID*F****DESCRIPTION FOR ITEM 4~
  PO1**100*CA*18**UK*78945612345678~
  PID*F****THIS IS ITEM 5~
  CTT*5~
  SE*45*12345648~
  GE*2*1234~
  IEA*1*000025011~
  ISA*00*          *00*          *AA*PPPPPP         *11*4165555555     *999999*0002*P*00123*000000021*0*R*>~
  GS*PO*4167913211*4162475478*20100917*0019*0200*X*001111PQRS~
  ST*150*2345678~
  BEG*00*SA*8888888891**20130917~
  DTM*119*20131001~
  DTM*004*20131002~
  N9*AH*1234034034~
  N1*ST*D072 LOC*9*2471916380072~
  N3*2700 NAME~
  N4*CITY*QQ*ABC DEF*CA~
  PO1**2*CA*40.10**UK*10060383002975~
  CTP**UCP*40.10~
  PID*F****DESCRIPTION~
  PO1**20*CA*10**UK*21234567897450~
  PO1**30*CA*11**UK*45678912345678~
  PO1**50*CA*22.05**UK*1234567895458~
  CTP**UCP*22.38~
  PO1**80*CA*20**UK*4567894578454~
  SE*40*50501231~
  GE*1*1234~
  IEA*1*000014052~

Open in new window



I would like to add a Location to the output.

Currently, the output displays similar to this:

PONumber: 1234561234
 Item 1: UPC: 12345678912345, Desc: information here, Qty: 248 Price: $10.00
 Item 2: UPC: 23456789123452, Desc: information here, Qty: 329 Price: $1.00
 Item 3: UPC: 98765432198765, Desc: information here, Qty: 20 Price $2.00
 Total: $Calculated total

Open in new window


I would like the output to look like this, notice the Ship To information.

 PONumber: 1234561234
 Ship To:  Street, Location
 Item 1: UPC: 12345678912345, Desc: information here, Qty: 248 Price: $10.00
 Item 2: UPC: 23456789123452, Desc: information here, Qty: 329 Price: $1.00
 Item 3: UPC: 98765432198765, Desc: information here, Qty: 20 Price $2.00
 Total: $Calculated total

Open in new window


NB - The Ship To information comes from the 2nd instance of the N3 and the N4

For example,
In the first set of data between ISA and IEA, the 2nd instance of N3 and N4 looks like this:
  N3*Street~
  N4*Location*BB*ABC DEF*CA~

Open in new window


The second line in the output data needs to pickup the Street and the Location after the first asterisk.
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
  • 4
5 Comments
 

Author Comment

by:100questions
ID: 40389913
Any assistance would be appreciated.
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40390830
Try this version:
$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

output:
PO Number: 8888888887
Ship to: Street, Location
Item 1: UPC: 12345678912345, Desc: ITEM 1 DESCRIPTION, Qty: 10, Price: $12 
Item 2: UPC: 23456789123456, Desc: ITEM 2 DESCRIPTION, Qty: 50, Price: $20 
Item 3: UPC: 34567891234567, Desc: ITEM 3 DESCRIPTION, Qty: 30, Price: $14.03 
Item 4: UPC: 45678912345678, Desc: DESCRIPTION FOR ITEM 4, Qty: 10, Price: $20.50 
Item 5: UPC: 78945612345678, Desc: THIS IS ITEM 5, Qty: 100, Price: $18
Total: $3545.9

PO Number: 8888888891
Item 1: UPC: 10060383002975, Desc: DESCRIPTION, Qty: 2, Price: $40.10 
Item 2: UPC: 21234567897450, Desc: , Qty: 20, Price: $10 
Item 3: UPC: 45678912345678, Desc: , Qty: 30, Price: $11 
Item 4: UPC: 1234567895458, Desc: , Qty: 50, Price: $22.05 
Item 5: UPC: 4567894578454, Desc: , Qty: 80, Price: $20
Total: $3312.7

Open in new window

0
 

Author Comment

by:100questions
ID: 40392035
Works perfectly, thank you.
0
 

Author Closing Comment

by:100questions
ID: 40392038
Excellent.
0
 

Author Comment

by:100questions
ID: 40392908
I posted another question which essentially takes the same code but instead of using the 2nd instance of the N3 and N4, it needs to use the first instance of it.
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

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.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
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…

756 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