I could reformat the text.txt file into one line per record and use the ID to grep and combine the hits. I guess not very elegant and slow. I was also thinking of use a grep inside a while loop but I guess this would be slow too.
I was hoping to find an array solution but I would need some help:
awk 'FNR==NR{a[$1];next}{ for(i=1;i<=NF;i++){ if($i ~ "+" && $i in a) {print $i} else print $i} }' head.txt text.txt
thank you all for helping me to find three perfect solutions for my problem. @Aia, sorry if my description of the problem was not so clear.
Here the three solutions that worked for me:
time awk 'FNR==NR {h["+" $1]=$2;next} $1 in h {print $0, h[$1]; getline; print}' text.txt header.txt > outA.tmp
real 0m20.552s
user 0m17.823s
sys 0m2.697s
time awk '
NR == FNR {
i = substr($0,2);
getline;
A = $0
next
}
$1 in A {
print "+" $0 RS A[$1]
}
' text.txt head.txt > outB.tmp
real 0m22.600s
user 0m19.482s
sys 0m3.078s
time awk 'FNR==NR {h[$1]=$2;next} $1 in h {print "+" $1, h[$1];print $2}' header.txt RS="+" text.txt > outC.tmp
real 1m29.579s
user 1m25.451s
sys 0m3.584s