grep (BSD grep) 2.5.1-FreeBSD on mac os 10.8 line regexp 模式不适用于重叠模式 [英] grep (BSD grep) 2.5.1-FreeBSD on mac os 10.8 line regexp mode not working with overlapping patterns

查看:15
本文介绍了grep (BSD grep) 2.5.1-FreeBSD on mac os 10.8 line regexp 模式不适用于重叠模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
test2
" > f1.txt
printf "test
test2
" > f2.txt
grep -x -F -f f1.txt f2.txt

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

Has anyone else made similar observations or even knows about a workaround?

推荐答案

已验证;这确实看起来像 Apple 和/或 FreeBSD grep 中的错误.(这对我来说完全不足为奇;FreeBSD grep 是一堆讨厌标准的便便.我的雇主在我们所有的 Mac 系统上安装了 GNU grep,而不是忍受 FreeBSD 的古怪行为.是的,GNU grep 在您的示例中找到了这两行.)

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.)

如果 Mac 和 Linux 之间的可移植性是一个目标,我强烈建议完全放弃 grep 而只使用 perl(或类似 ack,这取决于 perl).

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 建议在 f2.txt 中将较长的模式放在较短的模式之前似乎可以解决该错误,但我对 依赖 之类的东西持怀疑态度.它适用于这个玩具示例,但它是否一般有效,当你发现时它会有多贵?

@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-FreeBSD on mac os 10.8 line regexp 模式不适用于重叠模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆