I have this file.txt.Z
which contains this:
AK2*856*1036~AK3*TD1*4**~AK4*2**1*~AK4*7**1*~AK3*TD5*5**~AK4*3**6*2~AK3*REF*6**~AK4*2**1*~AK3*REF*7**~AK4*2**1*~AK3*REF*8**~AK4*2**1*~AK3*DTM*9**~AK4*2**4*20~AK4*2**4*20~AK3*CTT*12**7~AK5*R
AK2*856*1037~AK3*HL*92**~AK4*3**7*O~AK5*R~AK9*R*2*2*0~SE*25*0001~GE*1*211582~IEA*1*000211582
Each record consists of several fields starting with a header (usually AK
with a number), separated by ~
. If you replace the ~
with an indented line break it would read:
AK2*856*1036
AK3*TD1*4**
AK4*2**1*
AK4*7**1*
AK3*TD5*5**
AK4*3**6*2
AK3*REF*6**
AK4*2**1*
AK3*REF*7**
AK4*2**1*
AK3*REF*8**
AK4*2**1*
AK3*DTM*9**
AK4*2**4*20
AK4*2**4*20
AK3*CTT*12**7
AK5*R
AK2*856*1037
AK3*HL*92**
AK4*3**7*O
AK5*R
AK9*R*2*2*0
SE*25*0001
GE*1*211582
IEA*1*000211582
Each field has subfields separated by *
. For example, subfield AK201
is the first field after an AK2
header, so it's 856
for the example lines.
As you can see there is 2 lines that has a starting string of AK2
. That is like a line header or as we call it, Segment Header. There are two segment header in file.txt.Z
. What I want is to get these data from each segment header in order:
Data Needed:
- AK202 (second field after
AK2
header) -AK2*856*this_numeric_value
before the asterisk or~
. - AK301 (first field after
AK3
header) -~AK3*this_string_value
before the*
or~
. - AK502 (second field after
AK5
header) -~AK5*some_string_value*this_numeric_value
before the*
or~
. - AK401 (first field after
AK4
header) -~AK4*this_numeric_value
before the*
or~
. - Every numerical value from
AK4
orAK5
field are should always be at least 2 digits. e.g. AK502 = 2; AK502 = 02 or AK401 = 9; AK401 = 09. - If there is no
AK3
field, don't output anything. (I have already a script for this) - If a line contains more than one AK3-AK5-AK4-sequences, they should get concatenated with a whitespace
- If the
AK5
field misses after theAK3
field, look forAK4
field instead. - If neither an
AK4
nor anAK5
field are present after theAK3
field, Only output the AK301(first field after AK3 header). - If there is more than one
AK4
field after anAK3
field, concatenate the AK502-AK401-sequences by commas
Output:
GS: 1036 - TD102,07 TD503 REF02 DTM02,02 CTT
GS: 1037 - HL03
How to do this? Just ask me if you're confused with my question.
Edit: This is my code: This is inside a while loop
while read FILE
do
AK2=`zgrep -oP 'AK2.[\w\s\d]*.\K[\w\s\d]*' < $FILE`
AK3=`zgrep -oP 'AK3.\K[\w\s\d]*' < $FILE`
AK5=`zgrep -oP 'AK5.[\w\s\d]*.\K[\w\s\d]' < $FILE`
AK5_ERROR=`if [[ $AK5 =~ ^[0-9]+$ ]]; then printf "%02d" $AK5 2> /dev/null; else 2> /dev/null; fi`
AK4=`zgrep -oP 'AK4.\K[\w\s\d]*' < $FILE`
AK4_ERROR=`if [[ $AK4 =~ ^[0-9]+$ ]]; then printf "%02d" $AK4 2> /dev/null; else 2> /dev/null; fi`
if [[ $AK3 ]]
then
if $AK5 2> /dev/null
then
echo "GS: $AK2 - $AK3$AK4_ERROR"
else
echo "GS: $AK2 - $AK3$AK5_ERROR"
fi
else
echo "Errors are not specified in the file."
fi
done < file.txt.Z
The problem with my original code is that It doesn't concatenate $AK3
and, $AK5
or $AK4
.