获得支持负向后查找的 GNU grep 等价物的最简单方法是什么?
我正在尝试 grep 遍历嵌套子目录中的一堆文件来查找正则表达式匹配;我的正则表达式需要负向后查找。
Perl 具有负向后查找功能,但据我所知 GNU grep 不支持负向后查找。
获得支持负向后查找的 GNU grep 等价物的最简单方法是什么?
(我想我可以用 Perl 编写自己的迷你 grep,但这似乎没有必要。我的 Perl Cookbook 副本包括 tcgrep 的源代码;这是我应该使用的吗?如果是,最新的在哪里版本?别告诉我我必须输入整个程序!)
I'm trying to grep through a bunch of files in nested subdirectories to look for regular expression matches; my regex requires negative lookbehind.
Perl has negative lookbehind, but as far as I can tell GNU grep doesn't support negative lookbehinds.
What's the easiest way to get an equivalent to GNU grep that supports negative lookbehinds?
(I guess I could write my own mini-grep in Perl, but that doesn't seem like it should be necessary. My copy of the Perl Cookbook includes source for tcgrep; is that what I should use? If so, where's the latest version? Don't tell me I have to type this entire program!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用ack! Ack 是用 Perl 编写的,因此它使用 Perl 的正则表达式引擎(默认情况下)。
消极的后视是
ack“(?(根据willert的评论)
Use ack! Ack is written in Perl so it uses Perl's regex engine (by default).
The negative look-behind is
ack "(?<!bad)boy"
(per willert's comment)感谢其他问题的评论。我发现在
grep
中使用-P/--perl-regexp
选项实验性地支持负向后查找,因此您可能仍然不需要使用不同的工具,如果您更喜欢继续使用grep
。顺便说一句,我首选的 grep 替代方案是
grin
(用 python 编写)。Thanks to a comment from other question. I've found that negative lookbehind is experimentally supported in
grep
with the-P/--perl-regexp
option, so you may still not need to use a different tool if you prefer to keep usinggrep
.By the way, my preferred alternative to
grep
isgrin
(which is written in python).