Learn Just One Perl Command
A while back I wrote a post Learn one sed command. In a nutshell, I said it’s worth learning sed just do commands of the form
sed s/foo/bar/ to replace “foo” with “bar.”
Dan Haskin and Will Fitzgerald suggested in their comments that instead of
perl -pe with the same command. The advantage is that you could use Perl’s more powerful regular expression syntax. Will said he uses Perl like this:
cat file | perl -pe "s/old/new/g" > newfile
I think they’re right. Except for the simplest regular expressions, sed’s regular expression syntax is too restrictive. For example, I recently needed to remove commas that immediately follow a digit and this did the trick:
cat file | perl -pe "s/(?<=\d),//g" > newfile
Since sed does not have the look-behind feature or
\d for digits, the corresponding sed code would be more complicated.
I quit writing Perl years ago. I don’t miss Perl as a whole, but I do miss Perl’s regular expression support.
Learning Perl is a big commitment, but just learning Perl regular expressions is not. Perl is the leader in regular expression support, and many programming languages implement a subset of Perl’s regex features. You could just use a subset of Perl features you already know, but you’d have the option of using more features.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)