![](/img/trans.png)
[英]Inner join two files based on one column in unix when row names don't match with sort
[英]AWK, Unix command: How to match two files using corresponding first column in unix command
我有兩個文件,第一個具有單列(具有重復的ID),第二個文件是三列文件,第一列是與第一個文件相同但唯一編號相同的ID,我想打印第二個文件中與第一個文件相對應的其余兩列ID。
示例:第一個文件:
IDs
1
3
6
7
11
13
13
14
18
20
第二檔:
IDs Freq Status
1 1 JD611
2 1 QD51
3 2
5
6
7 2
11 2
13 2
14 2
所需的輸出
1 1 JD611
3 2
6
7 2
11 2
13 2
13 2
14 2
18
20
您可以使用以下awk
:
awk 'NR==FNR{a[$1]=$2 FS $3; next} {print $1, a[$1]}' f2 f1
要跳過標題行,
awk 'FNR==1{next} NR==FNR{a[$1]=$2 FS $3; next} {print $1, a[$1]}' f2 f1
如果第二個文件有多列,
awk 'NR==FNR{c=$1; $1=""; a[c]=$0; next} {print $1, a[$1]}' f2 f1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.