Solved

How can I use a Powershell script for the desired output?

Posted on 2014-10-29
4
94 Views
Last Modified: 2014-10-30
Data source:


ISA*00*          *00*          *00*1234567891     *ZZ*COMPANYINC     *123456*0000*P*00100*123456789*0*P*>~
GS*FA*1234567891*1234567894*20141029*1234*00*X*123456ABCD~
ST*567*0001~
AK1*PO*123~
AK2*888*000000123~
AK5*A~
AK9*A*1*1*1~
SE*0000*0000~
GE*1*89~
IEA*1*123456789~

Output file desired:

For each dataset it find between ISA and IEA, I would like the desired output:

Company:  COMPANYINC  (these are the words next found next to ZZ)
Type of document:  567  (these are the numbers next to the ST)
Document generated for a:  888 (the number found in the AK2)
Status:  A - (the letter found in the AK5) also on this line add - A (Accepted) R (Rejected)  
Status of Line Items:  A - , 3 Line Items (letter found in AK9, then count the amount of 1* found in the line)
Control Number:  123456789 (this is the number show in the IEA
0
Comment
Question by:100questions
  • 2
  • 2
4 Comments
 
LVL 10

Expert Comment

by:JoeKlimis
ID: 40412043
Hi

Please try the following  , you will need to update the name of the data file at line 2

let me know how you get on.



 
$dataFile		= "data1.txt"
$resultsFile	= "Results1.txt"
out-file $resultsFile  -encoding ascii
$data 		 	= get-content $dataFile
$result			= ""
write-output "Processing $($data.count) lines of data"
$count  = 0
#Company:  COMPANYINC  (these are the words next found next to ZZ)
# Type of document:  567  (these are the numbers next to the ST)
# Document generated for a:  888 (the number found in the AK2)
# Status:  A - (the letter found in the AK5) also on this line add - A (Accepted) R (Rejected)  
# Status of Line Items:  A - , 3 Line Items (letter found in AK9, then count the amount of 1* found in the line)
# Control Number:  123456789 (this is the number show in the IEA 

foreach ($line in $data)
{
	$SplitLine =$line.split("*")
	switch ( $splitLine[0] )
	{
	"ISA" 	{
			$company = $splitline[8]
			$doctype=""
			$docfor=""
			$status=""
			$statusofline = ""
			$statusCount =""
			$control=""
			}

	"ST" 	{ 	$count++
				$doctype  =  $splitline[1]}
	"AK2" 	{ 
				$docfor = $SplitLine[1]
				if ($count -gt 1) {" " | out-file $resultsFile -append -encoding ASCII  }
			}
			
	"AK5" 	{ 	
				$status = $SplitLine[1].split("~")[0] 
				$Quantity = $SplitLine[3] 
			}
	"AK9" 	{ 
				 	$statusofline = $SplitLine[1]
					$statuscount = $splitline.count -2
			}
	
	"IEA" 	{ 
			$Control = $SplitLine[2].split("~")[0]
			"Company: $company" | out-file $resultsFile -append -encoding ASCII
			"Type of document:  $doctype" | out-file $resultsFile -append -encoding ASCII
			"Document generated for a: $docfor" | out-file $resultsFile -append -encoding ASCII
			"Status: $statusofline" | out-file $resultsFile -append -encoding ASCII
			"Status of Line Items:  $statusofline, $statuscount" | out-file $resultsFile -append -encoding ASCII
			"Control Number:  $control" | out-file $resultsFile -append -encoding ASCII
			}
	}
}

Open in new window

0
 

Author Comment

by:100questions
ID: 40413071
Thanks Joe.  Will your script be able to list any additional instances of AK2 and AK5 within each data set of ISA to IEA?
In other words if there was another line of AK2 and another line of AK5 following it, would it be able to list that as well?

Additionally, it seems that in the output, after the A in the status, for the AK5, the words A (Accepted) R (Rejected) does not follow the A status.  Can this be added to your script.

Thank you.
0
 
LVL 10

Accepted Solution

by:
JoeKlimis earned 500 total points
ID: 40413482
Hi
I have updated the script
 
$dataFile		= "data1.txt"
$resultsFile	= "Results1.txt"
out-file $resultsFile  -encoding ascii
$data 		 	= get-content $dataFile
$result			= ""
write-output "Processing $($data.count) lines of data"
$count  = 0
#Company:  COMPANYINC  (these are the words next found next to ZZ)
# Type of document:  567  (these are the numbers next to the ST)
# Document generated for a:  888 (the number found in the AK2)
# Status:  A - (the letter found in the AK5) also on this line add - A (Accepted) R (Rejected)  
# Status of Line Items:  A - , 3 Line Items (letter found in AK9, then count the amount of 1* found in the line)
# Control Number:  123456789 (this is the number show in the IEA 
$count=0
foreach ($line in $data)
{
	$SplitLine =$line.split("*")
	switch ( $splitLine[0] )
	{
	"ISA" 	{
			$company = $splitline[8]
			$doctype=""
			$docfor=""
			$status=""
			$statusofline = ""
			$statusCount =""
			$control=""
			}

	"ST" 	{ 	$count++
				$doctype  =  $splitline[1]}
	"AK2" 	{ 
				$docfor = $SplitLine[1]
				"Company: $company" | out-file $resultsFile -append -encoding ASCII
				"Type of document:  $doctype" | out-file $resultsFile -append -encoding ASCII
				"Document generated for A: $docfor" | out-file $resultsFile -append -encoding ASCII
			}
			
			
	"AK5" 	{ 	
				$status = $SplitLine[1].split("~")[0] 
				"Status: $status" | out-file $resultsFile -append -encoding ASCII
			}
	"AK9" 	{ 
				$statusofline = $SplitLine[1]
				$statuscount = $splitline.count -2
				"Status of Line Items:  $statusofline, $statuscount" | out-file $resultsFile -append -encoding ASCII
			}
	
	"IEA" 	{ 
				$Control = $SplitLine[2].split("~")[0]
				"Control Number:  $control" | out-file $resultsFile -append -encoding ASCII
				"" | out-file $resultsFile -append -encoding ASCII
			}
	}
}

Open in new window


which I tested against this dataset

ISA*00*          *00*          *00*1234567891     *ZZ*COMPANYINC     *123456*0000*P*00100*123456789*0*P*>~
GS*FA*1234567891*1234567894*20141029*1234*00*X*123456ABCD~
ST*567*0001~
AK1*PO*123~
AK2*888*000000123~
AK5*A~
AK9*A*1*1*1~
SE*0000*0000~
GE*1*89~
IEA*1*123456789
ISA*00*          *00*          *00*1234567891     *ZZ*COMPANYINC     *123456*0000*P*00100*123456789*0*P*>~
GS*FA*1234567891*1234567894*20141029*1234*00*X*123456ABCD~
ST*768*0001~
AK1*PO*123~
AK2*888*000000123~
AK5*R~
AK9*R*1*1*1~
AK5*A~
AK9*A*1*1*1~
AK5*A~
AK9*A*1*1*1*1*1*1*1*1*1*1~
SE*0000*0000~
GE*1*89~
IEA*1*123456789

Open in new window



and   the following results were produced

Company: COMPANYINC    
Type of document:  567
Document generated for A: 888
Status: A
Status of Line Items:  A, 3
Control Number:  123456789

Company: COMPANYINC    
Type of document:  768
Document generated for A: 888
Status: R
Status of Line Items:  R, 3
Status: A
Status of Line Items:  A, 3
Status: A
Status of Line Items:  A, 10
Control Number:  123456789
0
 

Author Closing Comment

by:100questions
ID: 40413572
Thanks.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
A procedure for exporting installed hotfix details of remote computers using powershell
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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