7

mac os 10.8 下的 grep 库存版本(版本信息:grep (BSD grep) 2.5.1-FreeBSD)在以行正则表达式模式 (-x) 运行时似乎存在共享公共前缀的模式问题。在下面的最小示例中,我希望 grep 报告“test”和“test2”,但只报告“test”。(同样适用于单词正则表达式模式 (-w))。

printf "test\ntest2\n" > f1.txt
printf "test\ntest2\n" > f2.txt
grep -x -F -f f1.txt f2.txt

有没有其他人做过类似的观察,甚至知道解决方法?

4

1 回答 1

5

Verified; this does look like a bug in Apple and/or FreeBSD grep. (This is completely unsurprising to me; FreeBSD grep is a standards-hating pile of poo. My employer installs GNU grep on all our Mac systems rather than put up with FreeBSD's wacky behavior. And yes, GNU grep finds both lines in your example.)

If portability between Mac and Linux is an objective, I strongly recommend abandoning grep altogether and just using perl (or something like ack, which depends on perl).

@akostadinov's suggestion to put longer patterns before shorter ones in f2.txt does seem to work around the bug, but I would be leery of relying on something like that. It works on this toy example, but does it work in general, and how expensive will it be when you find out?

于 2013-03-14T23:24:33.960 回答