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
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
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
$& 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
For daily tips on regular expressions, follow @RegexTip on Twitter.