AWK比较两个文件,如果匹配,则将第二个文件的新列添加到第一个文件 [英] AWK Compare two files and add new column from second file to first file if match
本文介绍了AWK比较两个文件,如果匹配,则将第二个文件的新列添加到第一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有问题我想比较两个文件.第一个文件是参考:
I have problem I would like to compare two files. First files is reference:
ABCA4 INHR
AMT INHR
BTK ONKO1
PAP ONKO2
第二个文件用于比较:
3 1:2 T ENG1 ABCA4 ff
3 1:2 T ENG1 ABCA4 gg
5 1:4 A ENG20 AMT ll
6 1:5 G ENG12 BRB ds
7 1:6 T ENG8 PAP rg
7 1:6 T ENG8 PAP tt
我想比较第一个文件中的 $1 和第二个文件中的 $5,如果第二个文件中的 $5 和 $6 之间存在匹配打印,则第一个文件中的 $2:
And I want compare $1 from first file with $5 from second file and if there is match print between $5 and $6 in second file the $2 from first file:
3 1:2 T ENG1 ABCA4 INHR ff
3 1:2 T ENG1 ABCA4 INHR gg
5 1:4 A ENG20 AMT INHR ll
6 1:5 G ENG12 BRB - ds
7 1:6 T ENG8 PAP ONKO2 rg
7 1:6 T ENG8 PAP ONKO2 tt
所有列都是制表符分隔的.谢谢
all columns are tab separated. Thanks
推荐答案
你可以这样做:
awk 'NR==FNR{a[$1]=$2;next}{$5=$5 " " (a[$5]?a[$5]:"-")}1' file1 file2
详情:
NR==FNR { # when the first file is processed
a[$1]=$2 # store the second field in an array with the first field as key
next # jump to the next record
}
{
$5=$5 " " (a[$5]?a[$5]:"-") # append a tab and the corresponding second
# field from the first file if it exists or -
}
1 # true, display the line
这篇关于AWK比较两个文件,如果匹配,则将第二个文件的新列添加到第一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文