Awk when matching and printing two files based on one column

I have two files (separator 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 files file1 and file2 and print (if there is a match) column 1 to 5 of db.txt

and column 1query.txt

I tried using awk, here's 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 file columns db.txt

EDIT: mine is more complexdb2.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

      

+3


source to share


2 answers


You can use awk

like this:



awk 'BEGIN{FS=OFS="\t"} FNR == NR { a[$2] = $1; next }
$4 in a { print $0, a[$4] }' query.txt db.txt

string1 string2 string3 001 string4 id1
string9 string10 string11 003 string12 id2

      

+1


source


AMD$ cat f1
id1 001
id2 003

AMD$ cat f2
string1 string2 string3 001 string4
string5 string6 string7 002 string8
string9 string10 string11 003 string12

AMD $ awk 'NR==FNR {a[$2]=$1; next} {for(i in a) if(index($0,i)) print a[i], $0}' f1 f2
id1 string1 string2 string3 001 string4
id2 string9 string10 string11 003 string12

      



+1


source







All Articles