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



我想在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可能在MacOS上存在错误。我在这个测试用例中使用grep --color=never,因为它也是 MacOS 上的错误 grep -f 在 OS X 上产生段错误

在这种情况下,我通过这样做在 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]$

最新更新