Solved

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

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

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

Suggested Solutions

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

791 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