Parse File on delimiter and create new file with data

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.

20210916 ~16-SEP-21~14907703 ~421000172 ~ 1~PON-EMERGENCY^02^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14907743 ~421000172 ~ 1~PON-EMERGENCY^02^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14907745 ~425000007 ~ 1~PON-EMERGENCY^01^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14907801 ~421000172 ~ 1~PON-EMERGENCY^06^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~15-SEP-21~14907566 ~425000744 ~ 1~PON-EMERGENCY^05^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14907952 ~421000172 ~ 1~PON-EMERGENCY^02^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14908622 ~421001147 ~ 1~PON-EMERGENCY^06^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14909780 ~425000074 ~ 1~PON-EMERGENCY^02^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14909780 ~425001914 ~ 1~PON-EMERGENCY^02^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14909780 ~425001098 ~ 1~PON-EMERGENCY^02^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14909780 ~425000744 ~ 1~PON-EMERGENCY^02^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14909850 ~421006924 ~ 1~PON-EMERGENCY^04^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14909850 ~421001626 ~ 1~PON-EMERGENCY^04^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14909850 ~425000074 ~ 1~PON-EMERGENCY^04^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14909917 ~422005054 ~ 1~PON-EMERGENCY^06^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14910174 ~421000172 ~ 1~PON-EMERGENCY^02^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14910182 ~421000172 ~ 1~PON-EMERGENCY^05^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14910217 ~421000172 ~ 1~PON-EMERGENCY^06^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14910258 ~421000172 ~ 1~PON-EMERGENCY^03^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14910243 ~425000074 ~ 1~PON-EMERGENCY^01^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14910243 ~425000744 ~ 1~PON-EMERGENCY^01^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14910243 ~425001922 ~ 1~PON-EMERGENCY^01^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14910243 ~430025188 ~ 1~PON-EMERGENCY^01^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14910270 ~421000172 ~ 1~PON-EMERGENCY^OBS^A^NMMC-PONTOTOC ~SRIS-RAD
20210916 ~16-SEP-21~14910270 ~421000016 ~ 1~PON-EMERGENCY^OBS^A^NMMC-PONTOTOC ~SRIS-RAD

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.

Thanks,
Cole

Hi Cole, welcome to the forum :wave:

You’re right, you didn’t explain it well :slight_smile:

Can you give an example of what the output should look like?

Also, if you have any code you’ve tried so far, it might be helpful to post that too.

Please make use of the </> when posting your examples; it helps greatly with readability.

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.

Something like this should work:

$chargeAccounts = Get-Content E:\Temp\Files\cole.txt
$outFile = 'E:\Temp\Files\coleTest.txt'

foreach ($chargeAccount in $chargeAccounts) {
    $splits         = $chargeAccount -split '~'
    $BusinessDate   = (Get-Date).AddDays(-1).ToString('yyyyMMddHHmm')
    $ServiceDate    = $splits[1] | Get-Date -Format('yyyyMMdd') 
    $AccountNumber  = $splits[2]
    $ItemNumber     = $splits[3]
    $Qty            = $splits[4]
    $Location       = $splits[5]
    $Dept           = $splits[3].Substring(0,4)
    $SystemofOrigin = $splits[6]

    Add-Content -Path $outFile -Value "MSH|^~\&|||||$BusinessDate|||$BusinessDate|||||"
    Add-Content -Path $outFile -Value "EVN|P03|$BusinessDate|$BusinessDate" |
    Add-Content -Path $outFile -Value "PID||||||||||||||||||$AccountNumber"
    Add-Content -Path $outFile -Value "FT1||||$ServiceDate|$ServiceDate|CH|$ItemNumber|||$Qty|||$Dept|||-EDI~~|||||||||||~PHESD-"
}

Okay that got me much closer but I am getting this error:
Add-Content : Stream was not readable.
At line:14 char:5

  • Add-Content -Path $outFile -Value "MSH|^~\&|||||$BusinessDate|||$ ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ok, something is interfering with the file. Maybe AV software. Perhaps try Out-File instead.

$chargeAccounts = Get-Content E:\Temp\Files\cole.txt
$outFile = 'E:\Temp\Files\coleTest.txt'

foreach ($chargeAccount in $chargeAccounts) {
    $splits         = $chargeAccount -split '~'
    $BusinessDate   = (Get-Date).AddDays(-1).ToString('yyyyMMddHHmm')
    $ServiceDate    = $splits[1] | Get-Date -Format('yyyyMMdd') 
    $AccountNumber  = $splits[2]
    $ItemNumber     = $splits[3]
    $Qty            = $splits[4]
    $Location       = $splits[5]
    $Dept           = $splits[3].Substring(0,4)
    $SystemofOrigin = $splits[6]

$HL7 = @"
MSH|^~\&|||||$BusinessDate|||$BusinessDate|||||
EVN|P03|$BusinessDate|$BusinessDate
PID||||||||||||||||||$AccountNumber
FT1||||$ServiceDate|$ServiceDate|CH|$ItemNumber|||$Qty|||$Dept|||-EDI~~|||||||||||~PHESD-
"@ 

    $HL7 | Out-File -Path $outFile -Append -NoClobber

}
1 Like

Okay…now it is repeating again. See results below:

FT1||||20210916|20210916|CH|421000172                                         |||         1|||4210|||-EDI~~|||||||||||~PHESD-
MSH|^~\&|||||202109161256|||202109161256|||||
EVN|P03|202109161256|202109161256
PID||||||||||||||||||14907703            
FT1||||20210916|20210916|CH|421000172                                         |||         1|||4210|||-EDI~~|||||||||||~PHESD-
MSH|^~\&|||||202109161256|||202109161256|||||
EVN|P03|202109161256|202109161256
PID||||||||||||||||||14907703            

Disregard my last post I put :

$splits         = $chargeAccounts -split '~'

Instead of:

$splits         = $chargeAccount -split '~'

Thank you so much that got me to where I needed to be. I really appreciate your help. You are awesome!!

No problem, happy to help.