简体   繁体   中英

Vlookup in awk: how to list anything occuring in file2 but not in file1 at the end of output?

I have two files, file 1:

1    800     800     0.51
2    801     801     0.01
3    802     802     0.01
4    803     803     0.23

and file 2:

1    800     800     0.55
2    801     801     0.09
3    802     802     0.88
4    804     804     0.24

I have an awk script that looks in the second file for values that match the first three columns of the first file.

$ awk 'NR==FNR{a[$1,$2,$3];next} {if (($1,$2,$3) in a) {print $4} else {print "not found"}}' f1 f2
0.55
0.09
0.88
not found

Is there a way to make it such that any rows occurring in file 2 that are not in file 1 are still added at the end of the output, after the matches, such as this:

0.55
0.09
0.88
not found
4    804     804     0.24

That way, when I paste the two files back together, they will look something like this:

1 800 800 0.51 0.55
2 801 801 0.01 0.09
3 802 802 0.01 0.88
4 803 803 0.23 not found
4 804 804 not found 0.04

Or is there any other more elegant solution with completely different syntax?

 awk '{k=$1FS$2FS$3}NR==FNR{a[k]=$4;next}
      k in a{print $4;next}{print "not found";print}' f1 f2

The above one-liner will give you:

0.55
0.09
0.88
not found
4 804 804 0.24

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM