linux中实现两个文件按照指定列合并

发布时间 2023-09-13 17:44:44作者: 小鲨鱼2018

 

001、

[root@pc1 test01]# ls
file1  file2
[root@pc1 test01]# cat file1
a rs1
b rs2
c rs4
[root@pc1 test01]# cat file2
a 0.170721 -1.82031 0.0690841 rs1
b 0.0803412 -1.01669 0.309605 rs2
c 0.21245 0.22964 0.81843 rs3
d 0.0707158 -0.499683 0.617435 rs4
[root@pc1 test01]# awk 'NR == FNR {ay[$2]; next} $NF in ay' file1 file2
a 0.170721 -1.82031 0.0690841 rs1
b 0.0803412 -1.01669 0.309605 rs2
d 0.0707158 -0.499683 0.617435 rs4

 

002、

[root@pc1 test01]# ls
file1  file2
[root@pc1 test01]# cat file1
a rs1
b rs2
c rs4
[root@pc1 test01]# cat file2
a 0.170721 -1.82031 0.0690841 rs1
b 0.0803412 -1.01669 0.309605 rs2
c 0.21245 0.22964 0.81843 rs3
d 0.0707158 -0.499683 0.617435 rs4
[root@pc1 test01]# awk 'NR == FNR {ay[$2]; next} !($NF in ay)' file1 file2
c 0.21245 0.22964 0.81843 rs3