Solved

How can I use a Powershell script to output data for an Invoice

Posted on 2014-10-30
2
139 Views
Last Modified: 2014-11-03
Input Data, in a file called Source.txt

ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~


For each ISA to IEA data set in file, and there could be several, I would like the following
desired Output Data, in a file called Output.txt:

Company:  COMPANYCOL  (these are the words next found next to ZZ)
Type of document:  999  (these are the numbers next to the ST)
Identifier: 123000010  (the number found in IEA)

Then below the above output, I would like a blank line, and then I would like the data from the Source.txt to be displayed underneath it.

So the Output.txt file should look something like this, in this case:

Company:  COMPANYCOL
Type of document:  999
Identifier: 123000010


ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~
0
Comment
Question by:100questions
2 Comments
 
LVL 10

Accepted Solution

by:
JoeKlimis earned 500 total points
ID: 40413703
Good afternoon


Please try the following

 
$dataFile		= "data2.txt"
$resultsFile	= "Output.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
$lines=@()
foreach ($line in $data)
{
$lines+=$line
	$SplitLine =$line.split("*")
	switch ( $splitLine[0] )
	{
	"ISA" 	{
			$company = $splitline[8]
			$doctype=""
			$Ident=""
			}

	"ST" 	{ 	$count++
				$doctype  =  $splitline[1]}
	
	"IEA" 	{ 
				$ident = $SplitLine[2].split("~")[0]
				"Company:  $company"|out-file $resultsFile -append -encoding ASCII
				"Type of document:  $doctype"| out-file $resultsFile -append -encoding ASCII
				"Identifier: $ident"| out-file $resultsFile -append -encoding ASCII
				"" | out-file $resultsFile -append -encoding ASCII
				$lines | out-file $resultsFile -append -encoding ASCII
				$lines=@()
				"" | out-file $resultsFile -append -encoding ASCII
			}
	}
}

Open in new window



I tested this against this dataset
ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~
ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~
ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~

Open in new window


and achieved these results
Company:  COMPANYCOL    
Type of document:  999
Identifier: 123000010

ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~

Company:  COMPANYCOL    
Type of document:  999
Identifier: 123000010

ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~

Company:  COMPANYCOL    
Type of document:  999
Identifier: 123000010

ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~

Let me know how you get on
Joe
0
 

Author Closing Comment

by:100questions
ID: 40420218
Works very well, excellent.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Utilizing an array to gracefully append to a list of EmailAddresses
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

20 Experts available now in Live!

Get 1:1 Help Now