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

 

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、 提取file2中唯一的行

[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

 

 

原文:https://www.cnblogs.com/chenwenyan/p/13328122.html 

 

posted @ 2023-09-13 17:36  小鲨鱼2018  阅读(649)  评论(0)    收藏  举报