14

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?

1
  • 1
    Giving an example of what you're looking for would be helpful. And does grep with context help you find what you want (-C1)?
    – mbb
    Jul 7, 2011 at 13:59

3 Answers 3

16

Install Homebrew and then:

brew install pcre

This will give you the latest pcregrep

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.

2
  • I'm on OS X 10.11.2 and get "awk: invalid -v option"
    – HaggleLad
    Mar 17, 2016 at 10:02
  • in OS X you need a space between v and RS. i.e. awk -v RS='' '/112.*223/ { print "found it"; exit }' file.in
    – Neil
    Aug 29, 2018 at 20:14
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .