I have three files as an input and I need to rearrange this input to match the rules by which the processing program consumes the data.
My files are:
/tmp$ cat F[123]
# file -1-
FS00|0|zero-zero|
FSTA|0|10|
FSTA|0|12|
FSTA|0|15|
FSTA|0|17|
FS00|3|negative|
FSTA|3|-1|
FS00|5|regular|
FSTA|5|21|
#
GXRM|0|0|1|0
GXRX|120|200|220|220|
GXRX|116|202|222|222|
GXRM|2|1|0|0
GXRX|1|0|0|0|
#
STPE|Initial|
#
FNMR|20|12|11|12|11|0|
FNMR|21|15|10|10|10|5|
# file -2-
RGSS|0|1|1|1|1|0|
RGSS|1|1|1|1|1|2|
RGSS|7|6|1|9|2|2|
# file -3-
TESR|0.00|0.00|0|0|0.00|FIX
TESR|1.00|0.00|5|0|0.00|FIX CNT
TESR|4.20|1.05|5|5|0.10|FIX REV
TESR|0.00|0.00|8|7|0.00|FLEX
I need to combine them in this fashion:
STPE|Initial|
FS00|0|zero-zero|
FSTA|0|10|
FSTA|0|12|
FSTA|0|15|
FSTA|0|17|
FS00|3|negative|
FSTA|3|-1|
FS00|5|regular|
FSTA|5|21|
GXRM|0|0|1|0
GXRX|120|200|220|220|
GXRX|116|202|222|222|
GXRM|2|1|0|0
GXRX|1|0|0|0|
TESR|0.00|0.00|0|0|0.00|FIX
TESR|1.00|0.00|5|0|0.00|FIX CNT
TESR|4.20|1.05|5|5|0.10|FIX REV
TESR|0.00|0.00|8|7|0.00|FLEX
RGSS|0|1|1|1|1|0|
RGSS|1|1|1|1|1|2|
RGSS|7|6|1|9|2|2|
FNMR|20|12|11|12|11|0|
FNMR|21|15|10|10|10|5|
See, how I need groups of lines following each other, such as STPE to be 1st group, followed by FS group. Important to keep lines within each group in its original order.
I tried this awk (NOTE: gawk is not available, this is old awk) where I accumulate my groups of lines into arrays and I slap NR to be used later to re-sort output in the order these lines came in and I cut it off on the output.
/tmp$ cat f.awk
BEGIN { FS = OFS = "|" }
/^TESR/ { TESR[NR"|"$0] = 1; }
/^RGSS/ { RGSS[NR"|"$0] = 1; }
/^FNMR/ { FNMR[NR"|"$0] = 1; }
/^STPE/ { STPE[NR"|"$0] = 1; }
/^GX../ { GX__[NR"|"$0] = 1; }
/^FS../ { FS__[NR"|"$0] = 1; }
{
continue;
}
END {
for(ln in STPE) { print ln | "sort -n|cut -d'|' -f2-" }
for(ln in FS__) { print ln | "sort -n|cut -d'|' -f2-" }
for(ln in GX__) { print ln | "sort -n|cut -d'|' -f2-" }
for(ln in TESR) { print ln | "sort -n|cut -d'|' -f2-" }
for(ln in RGSS) { print ln | "sort -n|cut -d'|' -f2-" }
for(ln in FNMR) { print ln | "sort -n|cut -d'|' -f2-" }
}
The output is not at all what I expect:
/tmp$ awk -f f.awk F[123]
FS00|0|zero-zero|
FSTA|0|10|
FSTA|0|12|
FSTA|0|15|
FSTA|0|17|
FS00|3|negative|
FSTA|3|-1|
FS00|5|regular|
FSTA|5|21|
GXRM|0|0|1|0
GXRX|120|200|220|220|
GXRX|116|202|222|222|
GXRM|2|1|0|0
GXRX|1|0|0|0|
STPE|Initial|
FNMR|20|12|11|12|11|0|
FNMR|21|15|10|10|10|5|
RGSS|0|1|1|1|1|0|
RGSS|1|1|1|1|1|2|
RGSS|7|6|1|9|2|2|
TESR|0.00|0.00|0|0|0.00|FIX
TESR|1.00|0.00|5|0|0.00|FIX CNT
TESR|4.20|1.05|5|5|0.10|FIX REV
TESR|0.00|0.00|8|7|0.00|FLEX
Please help, any idea will be appreciated