gpt4 book ai didi

linux - grep -xf 与预期输出不匹配,可能是一个错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:56:55 26 4
gpt4 key购买 nike

我想在 a.txt 和 b.txt 中打印出相同的行。我认为“grep -xf a.txt b.txt”符合我的需要。但它不能正常工作。我的系统环境是MacOs Mojave

[yangyue ~/tempDir]$ cat a.txt
123 abc
123 abc jjj
123
456
zzz
[yangyue ~/tempDir]$ cat b.txt
123 abc
123 abc jjj
123
456def
456
xyz
[yangyue ~/tempDir]$ grep --color=never -xf a.txt b.txt
123 abc
123
456
[yangyue ~/tempDir]$

本例中的预期输出

123 abc
123 abc jjj
123
456

这两个文件的编码是一样的。每行末尾没有空格。我认为原因是“123 abc”是“123 abc jjj”的前缀然后我做了两个测试。

测试1

[yangyue ~/tempDir]$ cat 1.txt
a
ab
abc
[yangyue ~/tempDir]$ cat 2.txt
a
ab
abc
[yangyue ~/tempDir]$ grep --color=never -xf 1.txt 2.txt
a
[yangyue ~/tempDir]$

测试2

[yangyue ~/tempDir]$ cat 3.txt
abc
ab
a
[yangyue ~/tempDir]$ cat 4.txt
abc
ab
a
[yangyue ~/tempDir]$ grep --color=never -xf 3.txt 4.txt
abc
ab
a
[yangyue ~/tempDir]$

这是 grep 错误还是我的用法有误?

最佳答案

谢谢詹姆斯布朗

我找到了一个 Linux 服务器并进行了测试,我得到了预期的输出。 grep -xf 在 MacO 上可能有错误。我在这个测试用例中使用 grep --color=never,因为它也是 MacOs grep -f on OS X produces segfault 上的一个错误

在这种情况下,我通过这样做在 MacOs 上获得了预期的输出。但我不知道这是否是一个通用的解决方案。

[yangyue ~/tempDir]$ sort -r a.txt > c.txt
[yangyue ~/tempDir]$ sort -r b.txt > d.txt
[yangyue ~/tempDir]$ cat c.txt
zzz
456
123 abc jjj
123 abc
123
[yangyue ~/tempDir]$ cat d.txt
xyz
456def
456
123 abc jjj
123 abc
123
[yangyue ~/tempDir]$ grep --color=never -xf c.txt d.txt
456
123 abc jjj
123 abc
123
[yangyue ~/tempDir]$

关于linux - grep -xf 与预期输出不匹配,可能是一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953555/

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