gpt4 book ai didi

shell - 在 unix 中使用 join 比较两个文件

转载 作者:行者123 更新时间:2023-12-02 10:48:25 25 4
gpt4 key购买 nike

我正在Solaris unix上工作。我有两个文件:

> cat temp
2
8
6
> cat temp2
1
2
3
4
5
6
7
8
9
>

现在我需要 temp2 中存在于 temp 中的所有行:我遇到了this并尝试了以下命令。

> nawk 'FNR==NR{a[$0];next}($1 in a)' temp temp2
2
6
8
>

这非常有效。然后我还看到了使用 join here 的方法其中(以及手册页)说“默认情况下,当我们没有明确指定字段编号时,连接命令会匹配第一个字段上的文件”所以我想如果我给出 join temp temp2 我会得到

2
6
8

但这就是我得到的!

> join temp temp2
2
8
>

如果默认连接与第一列匹配,有人可以告诉我为什么这里缺少 6 吗?

最佳答案

您需要在运行 join 之前对文件进行排序才能匹配它们。

可以使用以下方法完成:

join <(sort temp) <(sort temp2)

我的输出是:

2
6
8

关于shell - 在 unix 中使用 join 比较两个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14134422/

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