paste two files according to concordance between columns paste two files according to concordance between columns shell shell

paste two files according to concordance between columns


You can indeed use awk for that:

awk 'NR==FNR{a[$1]=1} NR>FNR && a[$1]' file1 file2

NR==FNR{a[$1]=1} The a array filled with the content of file1.

NR>FNR && a[$1] is printing the line of file2 if the array contains the ID (aka $1).


If you like to use join, you can simply do:

join file file2

or , if input is tab separated and you use bash:

join -t $'\t' file1 file2


A more idiomatic awk solution would be :

awk 'NR==FNR{id[$1];next}$1 in id' file1 file2 >file3

Output

ID    []    p1    p2100    2.5    3.0    2.0102    2.6    4.0    2.5103    2.3    2.0    NA104    2.3    2.0    2.0108    3.2    2.0    4.0109    2.9    1.0    1.5112    3.1    2.5    2.0

References:

  1. awk [ referring to an array element ].
  2. awk [ next statement ].