Solved

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

Posted on 2014-10-29
4
92 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

10 Experts available now in Live!

Get 1:1 Help Now