You could try awk -f a.awk file2 file1
, where a.awk
is:
NR==FNR {
if (NR==1) {
split($0,b)
next
}
s="";
for (i=3; i<=NF; i++) {
if ($i!="NA") {
if (s)
s=s", " b[i]
else
s=b[i]
}
}
a[$1,$2]=s
next
}
FNR==1 {next}
($1,$2) in a {
print $1,$2,a[$1,$2]
}
Output:
234 789 hij, klm
678 780 abc
125 457 efg, hij, nmo
534 988 nmo