我有两个文件,两行不同。文件为
file1
0.156667 152.063604 17730.697726
100.256071 211.932169 14827.664042
108.604047 88.095015 12687.034619
11.868409 263.975480 16270.787931
126.014430 19.739961 19800.463965
file2
100.808 211.717 14.55 6002.643 16072.78
109.480 89.050 8.21 6002.197 8962.211
12.524 263.842 13.07 6001.377 11975.58
126.885 20.086 10.30 6002.365 16113.52
我想要的输入是
0.156667 152.063604 17730.697726 NA NA NA
100.256071 211.932169 14827.664042 14.55 6002.643 16072.78
108.604047 88.095015 12687.034619 8.21 6002.197 8962.211
11.868409 263.975480 16270.787931 13.07 6001.377 11975.58
126.014430 19.739961 19800.463965 10.30 6002.365 16113.52
因此我希望在+ - 2的误差范围内匹配前两列。并且基于此,匹配为正,我需要将第二个文件中的$ 3,$ 4和$ 5列附加到第一个(或创建一个新文件)。在没有匹配的情况下,我应该得到NA。
谢谢
答案 0 :(得分:2)
以下是使用awk
:
awk '
NR==FNR {
a[$1,$2]=$3 FS $4 FS $5;
next
}
{
for(x in a) {
split(x,t,SUBSEP);
if(t[1]-2<=$1 && $1<=t[1]+2 && t[2]-2<=$2 && $2<=t[2]+2) {
print $0, a[x]
next
}
}
print $0, "NA NA NA"
}' file2 file1
0.156667 152.063604 17730.697726 NA NA NA
100.256071 211.932169 14827.664042 14.55 6002.643 16072.78
108.604047 88.095015 12687.034619 8.21 6002.197 8962.211
11.868409 263.975480 16270.787931 13.07 6001.377 11975.58
126.014430 19.739961 19800.463965 10.30 6002.365 16113.52