猿问

如何使用bash合并两个文件并删除重复项?

我想知道如何在一个列上加入两个文件并删除重复项。首先举一些例子。


文件1:


SERVER1; Deployed; Infrastructure

SERVER2; Deployed; Infrastructure

SERVER3; Deployed; Infrastructure

SERVER4; Deployed; Infrastructure

SERVER5; Deployed; Infrastructure

文件2:


SERVER1;

SERVER2;

SERVER5;

期待:


SERVER3; Deployed; Infrastructure

SERVER4; Deployed; Infrastructure

尝试过类似的命令:sort File1 File2 | uniq > File3,但是它只返回合并的输出,因为它确实将每一列视为唯一,输出类似于:


 SERVER1;

 SERVER1; Deployed; Infrastructure

 SERVER2;

 SERVER2; Deployed; Infrastructure

 SERVER3; Deployed; Infrastructure

 SERVER4; Deployed; Infrastructure

 SERVER5;

 SERVER5; Deployed; Infrastructure

然后尝试使用命令从上面获得的内容中删除重复项awk -F";" '!_[$1]++' File3,但似乎只删除了一个重复行,而另一行是:


SERVER1;

SERVER2;

SERVER3; Deployed; Infrastructure

SERVER4; Deployed; Infrastructure

SERVER5;

我想检查重复项并删除重复项和服务器本身,您有什么建议吗?


交互式爱情
浏览 282回答 3
3回答

慕无忌1623718

该join命令在这里工作:$ join -t ';' -a 1 -v 2 File1 File2SERVER3; Deployed; InfrastructureSERVER4; Deployed; Infrastructure加入要求对文件进行排序。如果未排序:$ join -t ';' -a 1 -v 2 <(sort File1) <(sort File2)

胡说叔叔

以下内容awk可能对您有所帮助。awk&nbsp;'FNR==NR{a[$0];next}&nbsp;!($1&nbsp;in&nbsp;a)'&nbsp;File2&nbsp;&nbsp;File1

慕田峪9158850

您可以使用grep文件(-f)和(-v)反转匹配选项:grep -vf File2 File1输出:SERVER3; Deployed; InfrastructureSERVER4; Deployed; Infrastructure
随时随地看视频慕课网APP
我要回答