Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
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…
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, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

26 Experts available now in Live!

Get 1:1 Help Now