15

I want to do a simple regex search recursively through a directory that isn't restricted to single lines.

I tried grep and ack, but neither seem to support this.

I know it shouldn't be too computationally expensive (in my case) because my text editor of choice (Sublime Text) can do it just fine.

I'm just looking for a simple command-line version of this. Google has turned up pcregrep, but I can't seem to find that for Mac. Am I just not looking in the right place?

Dan Tao
  • 1,109

3 Answers3

16

Install Homebrew and then:

brew install pcre

This will give you the latest pcregrep

Tobia
  • 378
1

You didn't say exactly what you're looking for so I'll imagine the task to find out whether the file contains 112 followed by 223, disregarding newlines.

A slightly contrived awk solution:

awk -vRS='' '/112.*223/ { print "found it"; exit }' file.in

The RS variable contains the record separator. By setting it to the empty string, the whole file will be read as one record.

This will probably be very memory inefficient on large files.

By the way, I'm doing this on OpenBSD, but the OS X awk ought to work the same way.

Kusalananda
  • 2,369
  • 18
  • 26
0

"pcregrep" can refer to an older stable pcregrep and a newer pcre2grep. Both are provided by the PCRE project at pcre.org.

Mac Ports provides the former as part of port pcre (stable at version 8.40), and the latter as part of port pcre2 (currently version 10.22). Install via:

% sudo port install pcre2