简体   繁体   中英

AWK, Unix command: How to match two files using corresponding first column in unix command

I have two file, first with single column (with repeated IDs), second file is three columns file, first column is IDs which is same with first file BUT unique number, I want to print remaining two columns of second file corresponding to 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 this awk :

awk 'NR==FNR{a[$1]=$2 FS $3; next} {print $1, a[$1]}' f2 f1

To skip the 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

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