I am trying to use a similar command from awk compare columns from two files, impute values of another column , and looked a various questions that are similar to mine awk search column from one file, if match print columns from both files , How to import fields in other columns corresponding to one common field in two files with `NA` in all unmatched columns , awk compare 2 files, 2 fields different order in the file, print or merge match and non match lines with files that have more fields but I cannot get it to work. I also read from http://theunixshell.blogspot.com/2012/12/i-have-two-files-file-1-contains-3.html to see if it would work but I am still having trouble:
File 1:
xx NC1 12 13 ! pro
xy NC1 15 17 ! pro
yx NC1 18 20 ! pro
yy NC1 22 28 ! pro
File 2
xx ds
xy jt
yy wp
desired output:
xx NC1 12 13 ! pro ds
xy NC1 15 17 ! pro jt
yx NC1 18 20 ! pro NA
yy NC1 22 28 ! pro wp
The code I am using:
awk 'NR==FNR { a[$1]=$6; next }{print $0 " " ($2 in a ? a[$2] : "NA")}' file2 file1
So basically my output gives me a new column that are all "NA" which obviously is not what I am trying to get to.
output:
xx NC1 12 13 ! pro NA
xy NC1 15 17 ! pro NA
yx NC1 18 20 ! pro NA
yy NC1 22 28 ! pro NA