Perl One-Liners for Text Processing

The -e, -n, -p, -i and -a command-line flags that let a single line of Perl replace grep, sed, awk, and whole scripts.

  • perl
  • one-liners
  • command-line
  • text-processing

Perl on the command line replaces a surprising amount of grep, sed, and awk. Here are the flags and the idioms that matter.

The flags

  • -e — run the code that follows on the command line.
  • -n — wrap the code in a loop over every input line (no auto-print).
  • -p — like -n, but print each line automatically after running your code.
  • -i — edit files in place.
  • -l — handle line endings for you (strip on input, add on output).
  • -a — autosplit each line into @F (like awk fields); pair with -F to set the separator.

grep, but Perl

Print lines matching a pattern:

perl -ne 'print if /ERROR/' app.log

sed, but Perl

Substitute across a file and print the result:

perl -pe 's/colour/color/g' input.txt

Edit the file in place, keeping a backup:

perl -i.bak -pe 's/\bfoo\b/bar/g' config.txt

awk, but Perl

Sum the third whitespace-separated column:

perl -lane '$sum += $F[2]; END { print $sum }' data.txt

Print the first and last field of each comma-separated line:

perl -F, -lane 'print "$F[0] .. $F[-1]"' data.csv

Handy everyday one-liners

Count matching lines:

perl -ne '$c++ if /WARN/; END { print "$c\n" }' app.log

Number every line:

perl -pe '$_ = "$. $_"' file.txt

Strip trailing whitespace from a file in place:

perl -i -pe 's/\s+$/\n/' file.txt

Extract all email-looking strings:

perl -lne 'print $1 while /(\S+@\S+\.\w+)/g' contacts.txt

Why this is worth learning

Once these are muscle memory, you stop reaching for three different tools with three different syntaxes. One language — the same regular expressions you already know from Regular Expressions in Perl — handles matching, editing, and field processing in a single line.