比较两个文件并从第二个文件中获取匹配的行

时间:2013-10-16 06:54:30

标签: perl unix awk

文件1:

Locus        Gene name
chr1         AT1G27893.6
chr3         AT3G28270.3
chr4         AT5G46005.1
chr8         AT8G59300.5

文件2:

Gene name              Type                  Short_description
AT1G01010.1       protein_coding        AP2/B3-like transcriptional factor 
AT3G28270.3       protein_coding        NAC domain containing protein 1
AT8G59300.5       protein_coding        mitochondrial ribosomal protein S7
ATMG01270.1       protein_coding        FRIGIDA like 2  family member

输出:

基因座基因名称类型Short_description

chr3           AT3G28270.3        protein_coding        NAC domain containing protein 1
chr4           AT8G59300.5        protein_coding        mitochondrial ribosomal protein S7

我使用的代码

awk -F"," 'NR==FNR{a[$1]=$0;next} ($1 in a){ print a[$1]; next}1' file2 file1

我无法获得所需的输出,它在file2中打印所有行。

1 个答案:

答案 0 :(得分:1)

尝试使用join

join -1 2 -2 1 file1 file2

输出:

Gene Locus name name Type Short_description
AT3G28270.3 chr3 protein_coding NAC domain containing protein 1
AT8G59300.5 chr8 protein_coding mitochondrial ribosomal protein S7