How can I modify this PS script in order to produce the desired output?

Source data:

ISA*00*          *00*          *AA*COMPANYA       *00*1234567891     *123456*1234*A*00123*000012345*0*A*>~
GS*AB*COMPANYA*1234564567*20150619*1234*12345*A*001234~
ST*100*123456789~
BSN*00*00000123*20150701*1234*0000~
HL*1**A~
TD5*B*2*VIA*A*VIA2~
REF*AB*12345~
REF*AB*123456789-1~
N1*ST**00*0123~
HL*2*1*O~
PRF*0012-3456789-0123~
PID*A**AB*AB~
TD1*ABC00*00~
N1*AB**01*0123~
HL*3*2*A~
MAN*GM*12345678912345678912~
HL*1*2*A~
PO4*12~
MAN*UC*12345678945612~
HL*5*4*I~
LIN**AB*123456789~
SN1**13*BX~
HL*3*4*A~
PO4*0~
MAN*UC*78945612378945~
HL*1*2*A~
LIN**AB*123456789~
SN1**14*BX~
CTT*0~
SE*01*123456789~
ST*100*23456789~
BSN*00*00000123*20150619*1234*0000~
HL*2**B~
TD5*A*1*VIA2*V*VIA3~
REF*AB*12345~
REF*AB*123456789-0~
N1*ST**01*0456~
HL*4*3*A~
PRF*0012-3456789-0456~
PID*A**AB*CD~
TD1*ABC10*01~
N1*AB**01*0456~
HL*1*2*A~
MAN*GM*45678912345678945612~
HL*1*2*A~
PO4*10~
MAN*UC*78912345678945~
HL*1*2*A~
LIN**AB*456789123~
SN1**100*BX~
HL*1*2*B~
PO4*00~
MAN*UC*78945612345678~
HL*9*8*C~
LIN**AB*123456789~
SN1**11*AB~
CTT*1~
SE*01*123456789~
GE*02*12345~
IEA*0*123456789~
      ~

Open in new window


PS Script to modify:
$InputFile = "C:\source\source.txt"
$OutputFile = "C:\output\output.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 "(BSN\*)|(SN1\*)|(PID\*)|(SE\*)|N1"}
 }
 Process{
  $Data | % {
  #Check the end of data set
             If ($_ -match "N1\*"){
                   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,$n4 = $null,$null,$null,$null,$null,0,0,0
     }
  }
 #Collect the PO Number
  If($_ -match "PRF\*") {  
     $strArray = $_.split("*")
     $currpo = $strArray[3]
     $poline = "PO Number: $currpo"
  }
  #Collect details
  If($_ -match "SN1\*{1,}\d\*(\d{1,})\*\w{1,}\*(\d{1,}(\.\d{1,})?).*\*(\d{1,})~"){
     $count = $Matches[1]
		 $Price = $Matches[2]
		 $UPC = $Matches[4]
		 $total += [double]$count * [double]$Matches[2]
  }
  If($_ -match "PID\*"){
      $i++
      $Desc = $_ -Replace "PID\*\w\*{1,}(\d+\*{1,})?|<NL>|~"
      $line += "`r`nItem $i`: UPC: $UPC, Desc: $Desc, Qty: $count Price  `$$Price"
     }
   }
  }
 }

 If(Test-Path $InputFile){
  ParseText $OutputFile $InputFile
 }

Open in new window


Output file desired:
TITLE OF DOCUMENT:

Number.: 0012-3456789-0123
ITEM1: 12345678945612, Qty: 13
ITEM1: 78945612378945, Qty: 14

Number.: 0012-3456789-0456
ITEM1: 78912345678945, Qty: 100
ITEM1: 78945612345678, Qty: 11
100questionsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steven CarnahanNetwork ManagerCommented:
0
100questionsAuthor Commented:
@pony10us - Thanks however this was a different question.
0
Mauro CazabonnetSenior Software EngineerCommented:
Try this
#region
$InputFile = "C:\source\source.txt"
$OutputFile = "C:\output\output.txt"
#HashTable
$ProcessedPO = $null
$ReportedItems = $null
#endregion

Function ParseText ($OutputFile,$InputFile){
Begin{
	$script:ProcessedPO = @{}
	$script:ReportedItems = @{}	
	$line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
 	Set-Content $OutputFile $null
	"TITLE OF DOCUMENT:`r`n" | out-file $OutputFile -Encoding UTF8	
 	$Data = Get-Content -Path $InputFile | ?{$_ -match "(BSN\*)|SN1|(PID\*)|(SE\*)|N1|PRF|MAN"}
 	}
 	Process{
  	$Data | % {
  	#Check the end of data set_
 	#Collect the PO Number
  	If($_ -match "PRF\*") {
     	$strArray = $_.split("*")
     	$currpo = $strArray[1]
		$currpo = "PO Number: " + $currpo.Substring(0,$currpo.Length-1)
  	}
			
	#Collect details
  	If($_ -match "MAN\*"){		
		If($_.Contains("UC")){
		$strArray = $_.split("*")
		$currItem = $strArray[2]
		if(!($script:ProcessedPO.ContainsKey($currpo)))
		{			
			$itemcnt = 1			
			$script:ProcessedPO.Add($currpo,1)
			$currItem = "ITEM" + $itemcnt + ": " + $currItem.Substring(0,$currItem.Length-1)
		}
		else
		{		
			$itemcnt++
			$currItem = "ITEM" + $itemcnt + ": " + $currItem.Substring(0,$currItem.Length-1)
		}			
		}
	}			
	If($_ -match "SN1\*"){
     	$strArray = $_.split("*")
		$total = $strArray[2]
		$ProcessedItem = $currItem + ", Qty: " + $total
  	}
	
  	If ($_ -match "N1\*"){
        If($currPO -ne $null -and $total -ne $null){
        	$CurrentItem = $CurrPO + "," + $currItem + ", Qty: " + $total
			if(!($script:ReportedItems.ContainsKey($CurrentItem)))
			{		
				$script:ReportedItems.Add($CurrentItem,1)		
			}		
			#reset the variables
            $line,$ProcessedItem,$poline,$shipto,$totline,$total,$i,$n3,$n4 = $null,$null,$null,$null,$null,$null,0,0,0
     	}
  	}
   }
  }
}

 If(Test-Path $InputFile){
  ParseText $OutputFile $InputFile
 }

foreach ($item in $ProcessedPO.Keys) {
	"$item`r" | out-file $OutputFile -Encoding UTF8 -Append	
	foreach ($procitem in $ReportedItems.Keys) {
		If($procitem -match $item){	
			($po,$rest) = $procitem.split(',',2)
			"$rest`r" | out-file $OutputFile -Encoding UTF8 -Append	
		}
	}
	"`r" | out-file $OutputFile -Encoding UTF8 -Append	
}

Open in new window

0
What were the top attacks of Q1 2018?

The Threat Lab team analyzes data from WatchGuard’s Firebox Feed, internal and partner threat intelligence, and a research honeynet, to provide insightful analysis about the top threats on the Internet. Check out our Q1 2018 report for smart, practical security advice today!

100questionsAuthor Commented:
Thanks, this works, however the order of the items is not consistent, sometimes it's listed as ITEM2, 3 and 1 and sometimes 2, 1..
0
Mauro CazabonnetSenior Software EngineerCommented:
I'll update to have it sorted
0
Mauro CazabonnetSenior Software EngineerCommented:
Should sort correctly :)

#region
$InputFile = "C:\source\source.txt"
$OutputFile = "C:\output\output.txt"
#HashTable
$ProcessedPO = $null
$ReportedItems = $null
$POcount = $null
#endregion

Function ParseText ($OutputFile,$InputFile){
Begin{
	$script:ProcessedPO = @{}
	$script:ReportedItems = @{}
	$script:POcount = @{}	
	$line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
 	Set-Content $OutputFile $null
	"TITLE OF DOCUMENT:`r`n" | out-file $OutputFile -Encoding UTF8	
 	$Data = Get-Content -Path $InputFile | ?{$_ -match "(BSN\*)|SN1|(PID\*)|(SE\*)|N1|PRF|MAN"}
 	}
 	Process{
  	$Data | % {
  	#Check the end of data set_
 	#Collect the PO Number
  	If($_ -match "PRF\*") {
     	$strArray = $_.split("*")
     	$currpo = $strArray[1]
		$hashpo = $currpo.Substring(0,$currpo.Length-1)		
		$currpo = "PO Number: " + $currpo.Substring(0,$currpo.Length-1)
  	}
			
	#Collect details
  	If($_ -match "MAN\*"){		
		If($_.Contains("UC")){
		$strArray = $_.split("*")
		$currItem = $strArray[2]
		if(!($script:POCount.ContainsKey($hashpo)))
		{			
			$itemcnt = 1			
			$script:ProcessedPO.Add($currpo,$itemcnt)
			$script:POCount.Add($hashpo,$itemcnt)			
			$currItem = "ITEM" + $itemcnt + ": " + $currItem.Substring(0,$currItem.Length-1)			
		}
		else
		{
			$poitemcnt = $POcount.Get_Item($hashpo)
			$poitemcnt++			
			$script:POcount.set_item($hashpo,$poitemcnt)
			$currItem = "ITEM" + $poitemcnt + ": " + $currItem.Substring(0,$currItem.Length-1)			
		}			
		}
	}			
	If($_ -match "SN1\*"){
     	$strArray = $_.split("*")
		$total = $strArray[2]
		$ProcessedItem = $currItem + ", Qty: " + $total
  	}
	
			
  	If ($_ -match "N1\*"){
        If($currPO -ne $null -and $total -ne $null){
        	$CurrentItem = $CurrPO + "," + $currItem + ", Qty: " + $total
			if(!($script:ReportedItems.ContainsKey($CurrentItem)))
			{		
				$script:ReportedItems.Add($CurrentItem,1)		
			}		
			#reset the variables
            $line,$ProcessedItem,$poline,$shipto,$totline,$total,$i,$n3,$n4 = $null,$null,$null,$null,$null,$null,0,0,0
     	}
  	}
   }
  }
}

 If(Test-Path $InputFile){
  ParseText $OutputFile $InputFile
 }

foreach ($item in $ProcessedPO.Keys) {
	"$item`r" | out-file $OutputFile -Encoding UTF8 -Append	
	foreach ($procitem in $ReportedItems.getEnumerator() | sort Key) {
		$curritem = $procitem.name
		If($curritem -match $item){	
			($po,$rest) = $curritem.split(',',2)
			"$rest`r" | out-file $OutputFile -Encoding UTF8 -Append	
		}
	}
	"`r" | out-file $OutputFile -Encoding UTF8 -Append	
}

Open in new window

0
100questionsAuthor Commented:
Looks much better.
Is there any way it could sort it in the order of the PO number?
If not, that is fine, it looks good.  
Thanks,
0
Mauro CazabonnetSenior Software EngineerCommented:
Easy enough

Update the end section

foreach ($item in $ProcessedPO.getEnumerator() | sort Key) {
	$item = $item.name
	"$item`r" | out-file $OutputFile -Encoding UTF8 -Append	
	foreach ($procitem in $ReportedItems.getEnumerator() | sort Key) {
		$curritem = $procitem.name
		If($curritem -match $item){	
			($po,$rest) = $curritem.split(',',2)
			"$rest`r" | out-file $OutputFile -Encoding UTF8 -Append	
		}
	}
	"`r" | out-file $OutputFile -Encoding UTF8 -Append	
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
100questionsAuthor Commented:
Excellent, thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.