[Solved] awk compare two different columns of two files and print all from both file

Hi,
I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this

File1

1       rs1     abc
3       rs4     xyz
1       rs3     stu

File2

1       kkk      rs1     AA      10
1       aaa      rs2     DD      20
1       ccc      rs3     EE      30
1       nnn      rs4     RR      40

OUTPUT should be like this

1       kkk      rs1     AA      10    abc
1       aaa      rs2     DD      20    NA
1       ccc      rs3     EE      30     stu
1       nnn      rs4     RR      40     NA

column 1 and 2 from file1 should match with column 1 and 3 of the file2 and print file2 with column 3 of matched file1.
Thanks much.

Try :

$ awk 'FNR==NR{A[$1 FS $2]=$3;next}{print ($1 FS $3 in A ) ? $0 OFS A[$1 FS $3] : $0 OFS "NA"}' file1 file2
1 Like

Thank you very much works perfect.