如何使用bash& amp;删除重复?

时间:2018-05-10 14:26:04

标签: linux bash join text

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

File1中:

.material-tooltip {
  padding: 10px 8px;
  font-size: 1rem;
  z-index: 2000;
  background-color: transparent;
  border-radius: 2px;
  color: #fff;
  min-height: 36px;
  line-height: 120%;
  opacity: 0;
  position: absolute;
  text-align: center;
  max-width: calc(100% - 4px);
  overflow: hidden;
  left: 0;
  top: 0;
  pointer-events: none;
  visibility: hidden;
  background-color: #323232;
  font-family: "Roboto Mono";
  font-size: 0.8em;
  font-weight: 700;
}

文件2:

SERVER1; Deployed; Infrastructure
SERVER2; Deployed; Infrastructure
SERVER3; Deployed; Infrastructure
SERVER4; Deployed; Infrastructure
SERVER5; Deployed; Infrastructure

期望:

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

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

2 个答案:

答案 0 :(得分:3)

关注awk可能对您有帮助。

awk 'FNR==NR{a[$0];next} !($1 in a)' File2  File1

答案 1 :(得分:2)

您可以将grep与文件(-f)和(-v)反转匹配选项一起使用:

grep -vf File2 File1

<强>输出:

SERVER3; Deployed; Infrastructure
SERVER4; Deployed; Infrastructure