How to Make Perl Regex One-Liners


Perl-one liners are powertools that get a lot of work done with a single command. You can use them on the Unix command-line—or indeed on the command line of any OS where Perl is installed. There are many pages about Perl one-liners. This one is only interested in Perl one-liners in the context of regular expression matching, replacing and splitting.

The site also has a page about Perl regex, but this one is only about one liners.

Perl One-Liner Recipes, not Regex Recipes

Other pages about Perl regex one-liners focus on showing you the regular expressions to accomplish certain tasks. In contrast, this page assumes you know regex, as teaching you regex is the focus of the rest of the site. What this page shows you is, given a certain regex, the Perl syntax to write one-liners to accomplish various tasks.

The idea is to get you up-and-running with Perl one-liners—not to get you up-and-running with regex.

Our Input
All our one-liners assume we are working with a file called yourfile containing these lines:
cat bat carrot
book true blue
red caramel 

To test the one-liners, I encourage you to create this file with nano or whichever tool you use to edit files.

Our Regex
All our one-liners assume we are working with this regex:
\bc\w+
The idea is to match words that start with the letter c. In our input, these words are cat, carrot and caramel.

General Notes on Syntax

These are for reference only, feel free to skip if you're not interested. Rather than spread out the explanations among the recipes, I gathered them in this short section.

✽ The -e (execute) flag is what allows us to specify the Perl code we want to run right on the command line. The Perl code is within quotes. Although it's possible to string several -e statements in a row, we won't do it here.
✽ The -n flag feeds the input to Perl line by line.
-0777 changes the line separator to undef, letting us to slurp the file, feeding all the lines to Perl in one go.
✽ All the examples assume we're working on one file called yourfile, but you could specify multiple files with yourfile yourfile2 yourfile3 or with *.txt
✽ All the examples assume we're working on one file called yourfile, but you could instead pipe some output to the one-liner, e.g. echo $PATH | perl…
✽ The -p flag (printing loop) processes the file line by line and prints the output.
✽ To replace directly in the file you can use the -i flag… but first test your one-liner without the -i to make sure it's what you want.
✽ If you're planning to use (*SKIP)(*F), remember this only works in Perl 5.10 and above: check your Perl version with perl -v
✽ The perl command is in apostrophes, and escaping those is hard work… So if your regex happens to contain apostrophes, first place it in an env variable then refer to it by name, e.g. env mypattern="'\w+" perl -0777 -ne 'while(m/$ENV{mypattern}/g){print "$&\n";}' yourfile


Tasks for Perl Regex One-Liners

We're ready to dive into the various types of tasks for Perl regex one-liners. If you have an idea for a task that's missing here, send me a comment.

As a reminder, we'll be working with this input:
cat bat carrot
book true blue
red caramel 

and this regex: \bc\w+ (which matches words that start with the letter c)


Task 1: Process the file line by line, and return all matches

We expect to match cat, carrot, caramel. Use this:
perl -ne 'while(/\bc\w+/g){print "$&\n";}' yourfile

Task 2: Process the file line by line, and return all matching lines

We expect to match lines 1 and 3. Use this:
perl -ne 'print if /\bc\w+/' yourfile

Task 3: Process the file line by line, and return the first match of each line

We expect to match cat and caramel. Use this:
perl -ne 'print "$&\n" if /\bc\w+/' yourfile

Task 4: Process the file as a block, and return all matches

We expect to match cat, carrot, caramel. Use this:
perl -0777 -ne 'while(m/\bc\w+/g){print "$&\n";}' yourfile

Task 5: Process the file as a block, and return the first match

We expect to match cat. Use this:
perl -0777 -ne 'print "$&\n" if /\bc\w+/' yourfile

Task 6: Process the file as a block, and replace all matches

To replace with ZAP, use this:
perl -0777 -pe 's/\bc\w+/ZAP/g' yourfile

Task 7: Process the file as a block, and replace the first match

To replace with ZAP, use this:
perl -0777 -pe 's/\bc\w+/ZAP/' yourfile

Task 8: Process the file line by line, and replace all matches

To replace with ZAP, use this:
perl -pe 's/\bc\w+/ZAP/g' yourfile

Task 9: Process the file line by line, and replace the first match

To replace with ZAP, use this:
perl -pe 's/\bc\w+/ZAP/' yourfile

Task 10: Process the file as a block, and split

Use this:
perl -0777 -ne 'if(@r=split(m/\bc\w+/,$_)){foreach(@r){print "$_\n";}}' yourfile

Task 11: Process the file line by line, and split

Use this:
perl -ne 'if(@r=split(m/\bc\w+/,$_)){foreach(@r){print "$_\n";}}' yourfile




Smiles,

Rex



Be the First to Leave a Comment






All comments are moderated.
Link spammers, this won't work for you.

To prevent automatic spam, we require that you type the two words below before you submit your comment.