Perl as a Better grep
Can Perl do better than grep when it comes to search files, without overcomplicating everything? The answer might surprise you.
Join the DZone community and get the full member experience.
Join For FreeI 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 -wln -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 -wln -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 -wln -e "/RE/ and print $&;" foo.txt
...because $&
is a special variable that holds the result of the latest match.
For daily tips on regular expressions, follow @RegexTip on Twitter.
Published at DZone with permission of John Cook, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments