Perl as a better grep

I like Perl’s pattern matching features more than Perl as a programming language. I’d like to take advantage of the former without having to go any deeper than necessary into the latter.

The book Minimal Perl is useful in this regard. It has chapters on Perl as a better grep, a better awk, a better sed, and a better find. While Perl is not easy to learn, it might be easier to learn a minimal subset of Perl than to learn each of the separate utilities it could potentially replace. I wrote about this a few years ago and have been thinking about it again recently.

Here I want to zoom in on Perl as a better grep. What’s the minimum Perl you need to know in order to use Perl to search files the way grep would?

By using Perl as your grep, you get to use Perl’s more extensive pattern matching features. Also, you get to use one regex syntax rather than wondering about the specifics of numerous regex dialects supported across various programs.

Let RE stand for a generic regular expression. To search a file foo.txt for lines containing the pattern RE, you could type

    perl -ln -e "/RE/ and print;" foo.txt

The Perl one-liner above requires more typing than using grep would, but you could wrap this code in a shell script if you’d like.

If you’d like to print lines that don’t match a regex, change the and to or:

    perl -ln -e "/RE/ or print;" foo.txt

By learning just a little Perl you can customize your search results. For example, if you’d like to just print the part of the line that matched the regex, not the entire line, you could modify the code above to

    perl -ln -e "/RE/ and print $&;" foo.txt

because $& is a special variable that holds the result of the latest match.

Update: If you’d like to use Perl regular expressions but you’d rather not write Perl code, you might like tcgrep. It uses Perl regular expressions but has an interface like grep.

3 thoughts on “Perl as a better grep

  1. In my opinion, the advantage of using Perl is that you could be more open-ended than using any software with a fixed set of options. Maybe you want to inject a little bit of programming logic in addition to your regex. Though if you want to completely avoid learning Perl then that’s not an option.

  2. You’re not the only one. Any time I find myself needing to check details on regexp syntax in bash, awk, etc. I just switch to Perl. It is, after all, pathologically eclectic.

Comments are closed.