I have the following data (it also contains other lines, here is a meaningful extract):
group
bb 1
cc 1
dd 1
end
group
dd 2
bb 2
end
group
aa 3
end
I don't know the values (like "1", "2", etc.) and have to match by the names (generic "group", "aa", etc.) I want to get the data filtered and sorted in the following order (with empty tabs when the string is absent):
group bb 1 cc 1 dd 1
group bb 2 dd 2
group aa 3
I run:
awk 'BEGIN {ORS = "\t"}\
/^group/ {print "\n" $0}; \
/^aa/ {AA = $0}; \
/^bb/ {BB = $0}; \
/^cc/ {CC = $0}; \
/^dd/ {DD = $0}; \
/^end/ {print AA; print BB; print CC; print DD}' test.txt
and get
group bb 1 cc 1 dd 1
group bb 2 **cc 1** dd 2
group aa 3 **bb 2** **cc 1** **dd 2**
which is in the right order, but the data is wrong (marked with asterisks). What is the correct way to do this filtering? Thanks!