Macos grep with-f选项根据模式文件的顺序返回不同的结果
我试图从另一个文件中的文本中匹配一个文件中列出的关键字。关键字文件以新行分隔Macos grep with-f选项根据模式文件的顺序返回不同的结果,macos,grep,gnu,Macos,Grep,Gnu,我试图从另一个文件中的文本中匹配一个文件中列出的关键字。关键字文件以新行分隔 grep根据关键字的顺序(而不是输入文件)返回不同的结果 这里有一个例子 有了这个输入 # input.txt TRITONS, sea-deities; they sung the marriage chorus of ACHILLES. 案例1 # terms.txt ACHILLES TRITONS # grep returns just one match grep -o -f terms.txt inpu
grep
根据关键字的顺序(而不是输入文件)返回不同的结果
这里有一个例子
有了这个输入
# input.txt
TRITONS, sea-deities; they sung the marriage chorus of ACHILLES.
案例1
# terms.txt
ACHILLES
TRITONS
# grep returns just one match
grep -o -f terms.txt input.txt
ACHILLES
案例2:我刚刚更改了关键字文件的顺序
# terms.txt
TRITONS
ACHILLES
# grep returns both matches !
grep -o -f terms.txt input.txt
TRITONS
ACHILLES
试图理解这种行为。我在
grep(BSD grep)2.5.1-FreeBSD
中发现了这个问题(这是Mac High Sierra上提供的默认版本)
改为使用Gnu Grep ggrep(Gnu Grep)3.4.
进行修复
通过brew安装grep
安装
请注意,这个版本的grep现在被称为
ggrep
您没有在任何地方使用input.txt
。已更新。我无法用GNU grep 3.4复制此内容-我使用Macgrep(BSD grep)2.5.1-FreeBSD
附带的两个版本的terms.txt
获得两个匹配项。我将尝试GNU grep。是的,这在GNU grepggrep(GNU grep)3.4中是不可复制的。无论顺序如何,此版本都会找到这两个术语。谢谢