grep(BSD grep)2.5.1-Mac OS上的FreeBSD 10.8行正则表达式模式不适用于重叠模式 [英] grep (BSD grep) 2.5.1-FreeBSD on mac os 10.8 line regexp mode not working with overlapping patterns
问题描述
printftest\\\
test2\\\
> ; f1.txt
printftest\\\
test2\\\
> f2.txt
grep -x -F -f f1.txt f2.txt
其他人做过类似的观察,甚至知道解决方法?
这看起来像Apple和/或FreeBSD grep中的错误。 (这对我来说完全没有什么意思; FreeBSD grep是一个令人厌恶的标准,我的雇主在我们所有的Mac系统上都安装了GNU grep,而不是忍受FreeBSD的古怪行为,是的,GNU grep在你的例子中找到了两行。 )如果Mac和Linux之间的可移植性是目标,我强烈建议完全放弃 grep
并仅使用 perl
(或类似于 ack
,这取决于 perl
)。
@ akostadinov建议在f2.txt中将更长的模式放在较短的模式之前似乎可以解决这个错误,但我会对 持怀疑态度。 >就像那样。它适用于这个玩具的例子,但是它的工作原理一般,当你发现它会有多昂贵?
It seems like the stock version of grep (version info: grep (BSD grep) 2.5.1-FreeBSD) under mac os 10.8 has an issue with patterns sharing a common prefix when being run in line regex mode (-x). In the following minimal example I would expect that grep reports "test" and "test2" but instead only "test" is reported. (Same also holds true for word regexp mode (-w)).
printf "test\ntest2\n" > f1.txt
printf "test\ntest2\n" > f2.txt
grep -x -F -f f1.txt f2.txt
Has anyone else made similar observations or even knows about a workaround?
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?
这篇关于grep(BSD grep)2.5.1-Mac OS上的FreeBSD 10.8行正则表达式模式不适用于重叠模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!