Issue
I have a script that runs through a set of HL7-files. From this script I'm trying to extract specific values from different segments of each file (MSH.7, PID.3, ORC.15 and OBR.46). The file looks like the example below (each file contains one long single line):
MSH|^~\&|SYSTEM|SENDER|RECEIVER|TST001^5790000192137^EAN|20230420170201.161+0100||OMG^O19^OMG_O19|OMGFPPQE54|P^T|2.5.1|||AL|AL||UNICODE UTF-8^MPID|||0101701111^^^&2.16.840.1.113883.3.4208.100.2&ISO||*********^*******||********|*|||************^^*****^^****^***^^^***||^***^**^^^^^^^^^********^MPD1|||^^^^^YNR^^^^12345^MAL1|1|OA^MORC|NW|14052154446^SENDER||1234567891012999^SENDER||||||||12345^Petersen^Peter C.^^^^^^YNR|||20230420154700||||||Lægehuset^^^^^YNR^^^^12345^MTQ1|||||||||R^MOBR||||UNSPECIFIED^Waiting visitation^99IMG|||||||||||||||||||||||||||^CT af thorax Gruppe 3\X0A\|||||||||||||||8001047^X-Ray Department^99SKS^MOBX||CE|OBS_010034^^99IMG||^.||||||O
I'm able to extract single values using below:
grep -Po "^MSH.*(?=PID)" | awk -F'|' '{ print $7 }'
But I'd like to be able to run through each file and extract values from each segment individually and add these values to a csv-file. A combination of the following that outputs individual values seperated with a comma to a csv-file:
grep -Po "^MSH.*(?=PID)" | awk -F'|' '{ print $7 }'
grep -Po "PID.*(?=PD1)" | awk -F'|' '{ print $7 }'
grep -Po "ORC.*(?=TQ1)" | awk -F'|' '{ print $7 }'
grep -Po "OBR.*(?=OBX)" | awk -F'|' '{ print $7 }'
I've tried using RS="MSH|PID|ORC|OBR"
, but have not been able to find a working solution. Anyone with a nice solution to this?
Expected output (hope this makes sense):
{MSH.7},{PID.3},{ORC.15},{OBR.46} // file 1 = 20230420170201.161+0100, 0101701111^^^&2.16.840.1.113883.3.4208.100.2&ISO, 20230420154700, 8001047^X-Ray Department^99SKS^MOBX
{MSH.7},{PID.3},{ORC.15},{OBR.46} // file 2
{MSH.7},{PID.3},{ORC.15},{OBR.46} // file 3
...
{MSH.7},{PID.3},{ORC.15},{OBR.46} // file n
Kind regards, Thomas
Solution
Here is one solution:
awk 'BEGIN {
FS = "|"
}
{
for (i = 1; i <= NF; i++) {
if ($i ~ "MSH") {
printf "%s, ", $(i + 6)
} else if ($i ~ "PID") {
printf "%s, ", $(i + 3)
} else if ($i ~ "ORC") {
printf "%s, ", $(i + 15)
} else if ($i ~ "OBR") {
printf "%s", $(i + 46)
}
}
printf " // %s\n", FILENAME
}' file*
20230420170201.161+0100, 0101701111^^^&2.16.840.1.113883.3.4208.100.2&ISO, 20230420154700, 8001047^Alb Røntgen Ambulatorium^99SKS^MOBX // file1.txt
Answered By - jared_mamrot Answer Checked By - Mildred Charles (WPSolving Admin)