gpt4 book ai didi

linux - 如何使用 bash 加入两个文件并删除重复项?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:17:31 25 4
gpt4 key购买 nike

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

文件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;

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

最佳答案

遵循 awk 可能会对您有所帮助。

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

关于linux - 如何使用 bash 加入两个文件并删除重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50275249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com