Solved

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

Posted on 2014-10-29
4
97 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Joe Klimis
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:
Joe Klimis 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Synchronize a new Active Directory domain with an existing Office 365 tenant
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

761 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