Friday, August 22, 2008

Multiline grep

I recently needed to do some multiline grepping through some log files. The problem is, the default grep and egrep on Linux systems don't seem to support regex patterns that extend over several lines.

Luckily, at least RHEL come with pcregrep installed, which does Perl-compatible regex matching. And by adding a -M switch you get multiline matching!

So you can just go
pcregrep -M 'a\nb' files...
Easy peasy!

4 comments:

Anonymous said...

Use ack! http://petdance.com/ack/

Anonymous said...

Thanks, pcregrep is exactly what I needed.

oc: I don't think ack does multiline matching, does it?

Dan Nuttle said...

Hm. I'm running RHEL 5.something, and I have pcregrep 4.5, but it doesn't support a -M option. You must have a newer version of pcregrep. Back to perl for me.

hognala said...

Thanks dude, exactly what I needed.