How can I modify this existing script to produce the desired output?

Source Data:
ISA*00*          *00*          *AA*COMPANYA       *00*1234567891     *123456*1234*A*00123*000012345*0*A*>~
GS*AB*COMPANYA*1234564567*20150619*1234*12345*A*001234~
ST*100*123456789~
BSN*00*00000123*20150701*1234*0000~
HL*1**A~
TD5*B*2*VIA*A*VIA2~
REF*AB*12345~
REF*AB*123456789-1~
N1*ST**00*0123~
HL*2*1*O~
PRF*0012-3456789-0123~
PID*A**AB*AB~
TD1*ABC00*00~
N1*AB**01*0123~
HL*3*2*A~
MAN*GM*12345678912345678912~
HL*1*2*A~
PO4*12~
MAN*UC*12345678945612~
HL*5*4*I~
LIN**AB*123456789~
SN1**13*BX~
HL*3*4*A~
PO4*0~
MAN*UC*78945612378945~
HL*1*2*A~
LIN**AB*123456789~
SN1**14*BX~
CTT*0~
SE*01*123456789~
ST*100*23456789~
BSN*00*00000123*20150619*1234*0000~
HL*2**B~
TD5*A*1*VIA2*V*VIA3~
REF*AB*12345~
REF*AB*123456789-0~
N1*ST**01*0456~
HL*4*3*A~
PRF*0012-3456789-0456~
PID*A**AB*CD~
TD1*ABC10*01~
N1*AB**01*0456~
HL*1*2*A~
MAN*GM*45678912345678945612~
HL*1*2*A~
PO4*10~
MAN*UC*78912345678945~
HL*1*2*A~
LIN**AB*456789123~
SN1**100*BX~
HL*1*2*B~
PO4*00~
MAN*UC*78945612345678~
HL*9*8*C~
LIN**AB*123456789~
SN1**11*AB~
CTT*1~
SE*01*123456789~
GE*02*12345~
IEA*0*123456789~
      ~

Open in new window


Current PS Script:
$dataFile = "C:\source\source.txt"
$resultsFile = "C:\output\output.txt"
out-file $resultsFile  -encoding ascii
$Header = "DATA:`r`n"
$Header | Out-File $resultsFile -Encoding ASCII
$data 		 	= get-content $dataFile
$result			= ""
write-output "Processing $($data.count) lines of data"
$count  = 0
foreach ($line in $data)
{
	$SplitLine =$line.split("~")[0].split("*")
	switch ( $splitLine[0] )
	{
	"BSN" 	{

            $PONumber = ""
			$Item = ""
			$Quantity=""

			}
	"PRF" 	{ 	
                $PONumber = $SplitLine[1]}


	"'MAN*UC*'" 	{ 
				$Item = $SplitLine[2]
                "Purchase Order No.: $($PONumber)" | out-file $resultsFile -append -encoding ASCII 
                "Item #: $($Item)" | out-file $resultsFile -append -encoding ASCII  
			}
	"SN1" 	{ 	
				$Quantity = $SplitLine[2] 
                "Purchase Order No.: $($Ponumber)" | out-file $resultsFile -append -encoding ASCII 
                "Item No: $($Item)" | out-file $resultsFile -append -encoding ASCII
                "Quantity: $($Quantity)" | out-file -filepath $resultsFile -append -encoding ASCII    
			}

	"SE" 	{ 
				if ($SplitLine[1] -eq "SE") 
				{

				}

			}
	}
}

Open in new window


Current sample of the type of output:

DATA:

Purchase Order No.: 0012-1234567-0123
Item No:
Quantity: 110
Purchase Order No.: 0013-1234567-0456
Item No:
Quantity: 10

Desired type of Output:

Output file desired:
 TITLE OF DOCUMENT:

 Number.: 0012-3456789-0123
 ITEM1: 12345678945612, Qty: 10
 ITEM2: 78945612378945, Qty: 12

 Number.: 0012-3456789-0456
 ITEM1: 78912345678945, Qty: 13
 ITEM2: 78945612345678, Qty: 15
100questionsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I've removed some unnecessary code too:
$dataFile = "C:\source\source.txt"
$resultsFile = "C:\output\output.txt"

get-content $dataFile | Select-String '^(BSN|PRF|MAN\*UC|SN1|SE)\*' | % {
  $SplitLine = $_.Line.split('~')[0].split('*')
  switch -exact ($SplitLine[0])
  {
    'BSN'  { $PONumber = $Item = $Quantity = '' }
    'PRF'  { Write-Output "`nNumber.: $($SplitLine[1])" }
    'MAN'  { if ($SplitLine[1] = 'UC') { $Item = $SplitLine[2] }}
    'SN1'  { Write-Output "ITEM: $Item, QTY: $($SplitLine[2])" }
  }
} | Out-File $ResultsFile -Encoding ASCII

Open in new window

Result is:
Number.: 0012-3456789-0123
ITEM: 12345678945612, QTY: 13
ITEM: 78945612378945, QTY: 14

Number.: 0012-3456789-0456
ITEM: 78912345678945, QTY: 100
ITEM: 78945612345678, QTY: 11

Open in new window

0
100questionsAuthor Commented:
Looks good however it's not leaving a space in between the last Item and the next PO number..

it's bunched up, something like this..

Number.: 0012-3456789-0123
ITEM: 12345678945612, QTY: 13
ITEM: 78945612378945, QTY: 14
Number.: 0012-3456789-0456
ITEM: 78912345678945, QTY: 100
ITEM: 78945612345678, QTY: 11
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I see, it works fine on the screen, but not as file output. We will have to add a CR:
$dataFile = "C:\source\source.txt"
$resultsFile = "C:\output\output.txt"

get-content $dataFile | Select-String '^(BSN|PRF|MAN\*UC|SN1|SE)\*' | % {
  $SplitLine = $_.Line.split('~')[0].split('*')
  switch -exact ($SplitLine[0])
  {
    'BSN'  { $PONumber = $Item = $Quantity = '' }
    'PRF'  { Write-Output "`r`nNumber.: $($SplitLine[1])" }
    'MAN'  { if ($SplitLine[1] = 'UC') { $Item = $SplitLine[2] }}
    'SN1'  { Write-Output "ITEM: $Item, QTY: $($SplitLine[2])" }
  }
} | Out-File $ResultsFile -Encoding ASCII

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
100questionsAuthor Commented:
Works excellent. Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.