views:

204

answers:

1

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

+11  A: 

Use ack! Ack is written in Perl so it uses Perl's regex engine (by default).

too much php
I thought someone might suggest this, but I don't think ack uses negative lookbehind. For example, I created a temp file in a temp directory containing two lines: "goodboy" and "badboy" and then used the sample from Perl in a Nutshell: `ack "(?<!=bad)boy"` ... it returned both lines. If the regex were being followed, only the "goodboy" line should return. (I'll reverse my downvote if you turn out to be right after all.)
Dan Fabulich
The negative look-behind is <code>ack "(?<!bad)boy"</code>, get away with the <code>=</code>
willert
Weird. I've written on my book in pen to rectify my error!
Dan Fabulich
WTF? "Vote too old to be changed, unless this answer is edited." @too much php, make an edit and I'll fix it, sorry. :-(
Dan Fabulich
I've fixed my vote.
Dan Fabulich