I have a file that I need to parse and get the data elements out of it. I can get the data elements but when I output them to a file I need them to be in a specific order but can’t seem to accomplish that.
I need to get each data element at the ~ and have it populate as specific elements. I may not be explaining this well so please forgive me. Any help would be greatly appreciated.
Sorry. I knew I didn’t do a good job but just wanted to get the ball rolling. Here is an example of what I tried:
$stream_reader = New-Object System.IO.StreamReader -ArgumentList $file
$stream_writer = New-Object System.IO.StreamWriter -ArgumentList $Test,$true
$line_number = 1
while (($current_line =$stream_reader.ReadLine()) -ne $null){
if($line_number -lt $ChargeAccounts.Length){
Foreach ($line in $ChargeAccounts) {
###########Each element out of line of array 9/15/21##################
$BusinessDate = $Time = (Get-Date).AddDays(-1).ToString('yyyyMMddHHmm')
$ServiceDate = $ChargeAccounts.split("~")[1][$line_number++] | Get-Date -Format('yyyyMMdd')
$AccountNumber = $ChargeAccounts.split("~")[2][$line_number++]
$ItemNumber = $ChargeAccounts.split("~")[3][$line_number++]
$Qty = $ChargeAccounts.split("~")[4][$line_number++]
$Location = $ChargeAccounts.split("~")[5][$line_number++]
$Dept = $ChargeAccounts.split("~")[3].Substring(0,4)[$line_number++]
$SystemofOrigin = $ChargeAccounts.split("~")[6][$line_number++]
#Build HL7Messages
$MSHSeg = "MSH|^~\&|||||$BusinessDate|||$BusinessDate|||||" #= "MSH|^~\&|||||$BusinessDate|||$BusinessDate|||||"
$EVNSeg ="EVN|P03|$BusinessDate|$BusinessDate"
$PIDSeg = "PID||||||||||||||||||$AccountNumber"
$FT1Seg = "FT1||||$ServiceDate|$ServiceDate|CH|$ItemNumber|||$Qty|||$Dept|||-EDI~~|||||||||||~PHESD-"
}
#Write Files to new file
$stream_writer.WriteLine($MSHSeg)
$stream_writer.WriteLine($EVNSeg)
$stream_writer.WriteLine($PIDSeg)
$stream_writer.WriteLine($FT1Seg)
$stream_writer.WriteLine()
$stream_writer.WriteLine()
}
}
$stream_reader.Dispose()
$stream_writer.Dispose()type or paste code here
This gives me the first data element repeated over and over but I need to loop through and build that same message but with the next data element. So for example:
In the data I sent previously I would want to populate each line into the MSH|EVN|PID|FT1 segments. Does that clarify at all?
Do you need to use StreamReader and StreamWriter? Nothing wrong with a programming approach where necessary but it would be simpler with just PowerShell commands.