I have two files (delimiter is tab)
File1: db.txt
string1 string2 string3 001 string4
string5 string6 string7 002 string8
string9 string10 string11 003 string12
File2: query.txt
id1 001
id2 003
and I wand to match file1 and file2 and print (if there is a match) column 1 to 5 of db.txt
and column 1 of query.txt
I tried using awk, here my code:
awk 'BEGIN{FS=OFS="\t"}NR==FNR{a[$2]=$4;next}$4 in a{print $1,$2,$3,$4,$5,a[$1]}' query.txt db.txt
but I only get a file with matches (? I at least think so) and columns of the db.txt
file
EDIT: my more complex db2.txt
string1 <TAB> string2 <TAB> 9999 abc dehi [way:pn9999] <TAB> 001 <TAB> org; string3 string4
string5 <TAB> string6 <TAB> 9999 dwd meti [way:pn8999] <TAB> 002 <TAB> org2; string7
string8 <TAB> string9 <TAB> 9999 dwd meti [way:pn7999] <TAB> 003 <TAB> org4; string10
db
file? - James Brownawk 'BEGIN{FS=OFS="\t"} FNR == NR { a[$1] = $1; next } $1 in a { print a[$2], $2 }'
- rororo