r - AWK, Unix command: How to match two files using corresponding first column in unix command -
i have 2 file, first single column (with repeated ids), second file 3 columns file, first column ids same first file unique number, want print remaining 2 columns of second file corresponding first file ids.
example: first file:
ids 1 3 6 7 11 13 13 14 18 20
second file:
ids freq status 1 1 jd611 2 1 qd51 3 2 5 6 7 2 11 2 13 2 14 2
desired output
1 1 jd611 3 2 6 7 2 11 2 13 2 13 2 14 2 18 20
you can use awk
:
awk 'nr==fnr{a[$1]=$2 fs $3; next} {print $1, a[$1]}' f2 f1
to skip header line,
awk 'fnr==1{next} nr==fnr{a[$1]=$2 fs $3; next} {print $1, a[$1]}' f2 f1
if second file has multiple columns,
awk 'nr==fnr{c=$1; $1=""; a[c]=$0; next} {print $1, a[$1]}' f2 f1
Comments
Post a Comment