I have a file with multiple lines which is structured as seen below
MSH|^~\&|Xatidok|V10.0.2.000|OSestra|x-tention|201203060855||ADT^A03|2914|P|2.3^AA&BB
EVN|A03|201203060855|201203060855|01|Fidani
PID|||00019380|2012049008^120005548^302830|PATIDOK-person^InRid^|Rudi|19111111|F|||Rose |A|Pens.
NK1||IRergrun^RROSlf^||Rose ^^Wels^^4600^A|07242123123|||||||||||||||||||||||||||||||
PV1||I|1212^G442^G442-||0|||||||||||2012049008|General|||||||||||||||||||12|||||201202060927|||||||
So basically there are rows with data on it seperated with pipes (|) and i want to parse them by writing a bash script.
So briefly this is the structure
- Segment > rows
- Field > cells between | field |
- Component > each field has (or doesnt) several fields seperated with ^
- Sub component > seperated with &
The idea of running the sript is: ./script.sh filename command
command should look like: MSH.2.3.4 or shorter
Meaning: Access the field which starts with MSH, Field number 2, Component number 3, Sub component 4
So my logic of parsing is as follows: I want to create an array which stores every row (segment) from the file as follows:
#!/bin/bash
file_to_be_parsed=$1
command=$2
counter=0
#read the file and split it into lines (segments) by creating an array called segments which holds all the lines (segment) in it
#array segments[] holds every line/segment of the file indexed from 0 to X
while IFS= read -a segment; do
segments[$counter]=$segment
counter=$((counter+1));
done < $file_to_be_parsed
SECOND: My second step is to seperate each array member one step further based on the delimiter and i can do it by:
IFS="|" read -r field <<< (here i can't figure out)
but i can't actually create 2D array in bash even though I searched a lot. Then i can access the specific fields ...
So can someone help me how to further seperate these array members into fields ...
MSH.2.3.4, find the line that starts withMSH, then split it and select the second element, then split that, etc.-Foption (use as-F\|and thensplit(), usingsplit(string,targArr,"^")(char to split by). Good luck.