Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 43 ยท Tools and Quality

Perl One-Liners

Learn Perl one-liners: the -e, -n, -p, -i, -l, -a and -F flags, in-place edits, practical text recipes, and how they compare with sed and awk.

Perl began life as a tool for chewing through text, and its command line still shows it. With a few flags you can filter lines, rewrite files, add up a column of numbers or reformat a log, all without creating a script file. These short programs are called one-liners, and they are the reason many administrators keep Perl installed.

This lesson explains the flags that matter (-e, -n, -p, -l, -a, -F, -i), builds a set of everyday recipes, and compares Perl with sed and awk so you can pick the right tool. The ideas build on Text Processing Idioms and the regex lessons.

Perl is not installed on this site, so the Perl commands below are read-only; their output was produced by running them on a real Perl. The interactive examples are bash stand-ins that do the same jobs with sed, awk and tr, which the site's runner has. Each stand-in is labelled, so you can see the equivalent Perl in the text.

The one-liner flags

Run Perl with -e to give it code on the command line instead of a file:

Terminal
perl -e 'print "Hello from Perl\n"'
TEXT
Hello from Perl

The real power comes from flags that wrap your code in a loop over the input. Here is a small file used by the examples that follow, called people.txt:

TEXT
ana 31 Lisbon
ben 27 Oslo
cara 45 Lisbon
dev 38 Pune
Flag Meaning
-e code Run this code. You can repeat -e; the pieces are joined with newlines.
-n Wrap the code in while (<>) { ... }: read each line into $_, print nothing automatically.
-p Like -n, but print $_ at the end of every pass.
-l Remove the newline from each input line (chomp) and add one back on every print.
-a Autosplit: with -n or -p, split each line on whitespace into the array @F.
-F pattern Set the split pattern for -a (a regex, such as -F: or -F,). Implies -a and -n in modern Perl.
-i[ext] Edit files in place. With an extension, keep a backup copy.
-0 / -00 Change the record separator; -00 reads whole paragraphs, -0777 the whole file at once.
-M module Load a module, as in -MList::Util=sum.
-w Turn on warnings.

The flags can be combined and placed together: -lane is the classic "read lines, strip newlines, split into fields, run my code" bundle, and -pi -e is the classic in-place edit. The -e must come last in a bundle because the code follows it.

Pick a loop flag

-n Read every line. You decide what to print.
-p Read every line and print it after your code.

Both put the current line in the variable $_.

1 / 2

Filtering with -n

With -n, Perl runs your code once per line, with the line in $_. A bare regex test and print is a filter, similar to grep:

Terminal
perl -ne 'print if /Lisbon/' people.txt
TEXT
ana 31 Lisbon
cara 45 Lisbon

The special variable $. holds the current line number, so you can number lines or select a range:

Terminal
perl -ne 'print "$.: $_"' people.txt
perl -ne 'print if 2..3' people.txt
TEXT
1: ana 31 Lisbon
2: ben 27 Oslo
3: cara 45 Lisbon
4: dev 38 Pune
ben 27 Oslo
cara 45 Lisbon

In print if 2..3, the two numbers are compared with $., so the lines between line 2 and line 3 inclusive are printed. Remember that the -n loop reads lines but prints nothing by itself, so anything you want to see needs a print.

Rewriting with -p

-p prints each line after your code runs, which makes it ideal for transformations. Change $_ and Perl prints the new value:

Terminal
perl -pe 's/Lisbon/Porto/' people.txt
TEXT
ana 31 Porto
ben 27 Oslo
cara 45 Porto
dev 38 Pune
Terminal
perl -pe 'tr/a-z/A-Z/' a.txt          # upper-case everything
perl -pe '$_ = "$.\t$_"' a.txt        # prefix each line with its number

Because the substitution operator works on $_ by default, s/old/new/ is a complete program body. All the regex features you know from Substitution and Transliteration are available here: captures, /g, /i, even /e to run code on the match.

Columns with -a, -F and -l

When your data has columns, -a splits each line on whitespace into @F. Perl arrays count from zero, so $F[0] is the first column and $F[-1] the last. Together with -l this gives you something like awk:

Terminal
perl -lane 'print "$F[0] is $F[1]"' people.txt
perl -lane 'print $F[-1]' people.txt
perl -lane 'print if $F[1] > 30' people.txt
TEXT
ana is 31
ben is 27
cara is 45
dev is 38
Lisbon
Oslo
Lisbon
Pune
ana 31 Lisbon
cara 45 Lisbon
dev 38 Pune

To split on something other than whitespace, add -F. The pattern is a regex, so -F: splits on colons and -F'\t' on tabs. A common job is reading a field from /etc/passwd-style files:

Terminal
echo "root:x:0:0" | perl -F: -lane 'print $F[0]'
TEXT
root

For totals, use an END block, which runs once after the last line:

Terminal
perl -lane '$sum += $F[1]; END { print $sum }' people.txt
perl -lane '$by{$F[2]}++; END { print "$_ $by{$_}" for sort keys %by }' people.txt
TEXT
141
Lisbon 2
Oslo 1
Pune 1

The second command counts rows per city with a hash, the same frequency-table idea from Hash Patterns. A matching BEGIN block runs before the first line, useful for headers or initial values.

Here are the same jobs done with awk and sed, which the site can run. They are stand-ins for the Perl commands above, shown side by side in comments:

Bash Stand-ins: filter, rewrite and total with sed and awk

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Editing files in place with -i

The -i flag turns -p into a file editor. Instead of printing to the screen, Perl writes the result back to the same file. Give -i an extension and it first saves the original with that extension:

Terminal
perl -i.bak -pe 's/Oslo/Bergen/' people.txt

After this, people.txt contains the change and people.txt.bak holds the old copy. Without a backup, a typo in your regex can destroy the file, so there are good habits:

  • Always run the command without -i first and check the output.
  • Use -i.bak until you trust the command, and use version control for anything important.
  • Several files at once work too: perl -i.bak -pe 's/foo/bar/g' *.txt.

On Windows cmd.exe, use double quotes around the code (and escape any inner double quotes), because single quotes are not quoting characters there. PowerShell and Unix shells both accept single quotes.

A recipe collection

A handful of patterns cover a surprising share of daily work. All were run on a real Perl with the people.txt file above.

Task One-liner
Count lines matching a pattern perl -lne '$c++ if /a/; END { print $c // 0 }' file
Print every line without blank ones perl -ne 'print unless /^\s*$/' file
Remove duplicate lines, keep order perl -ne 'print if !$seen{$_}++' file
Reverse every line perl -lne 'print scalar reverse' file
Show the longest line perl -ne '$m = $_ if length > length $m; END { print $m }' file
Print each line twice perl -ne 'print $_ x 2' file
Reverse the words on each line perl -lpe '$_ = join ",", reverse split' file
Sum a column with a module perl -MList::Util=sum -lane 'push @a, $F[1]; END { print sum(@a) }' file
Read paragraphs instead of lines perl -00 -ne 'print "para: $_"' file
Print a number sequence perl -le 'print for 1..3'

The duplicate remover deserves a closer look. $seen{$_}++ returns the old count (zero the first time, so !0 is true) and then increments it, so only the first occurrence of each line is printed. It is a compact version of the "set via hash" idiom, and unlike sort -u it keeps the original order.

For -00, the record separator is a blank line, so $_ holds a whole paragraph. With -0777 Perl "slurps" the entire file into $_, which lets a pattern match across line breaks.

Bash Stand-ins: dedupe, number and reverse (bash)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Perl, sed and awk: which one?

All three read text line by line, so the overlap is large. Roughly:

  • sed is perfect for simple substitutions and line deletions: sed 's/a/b/'. It is on every Unix system and fast to type, but it has no variables worth speaking of.
  • awk is built for columns and reports: awk '{ sum += $2 } END { print sum }'. It handles numbers and fields very naturally.
  • Perl does what both do and more: the full regex engine (lookahead, named captures, non-greedy quantifiers, /x), hashes, arrays, the whole CPAN, and the same behavior across systems. That consistency matters because sed -i and awk differ between GNU, BSD and macOS versions.

The trade-off is that Perl is slower to start than sed and the commands are longer. A good rule: use sed or awk when they fit in one short command, and switch to Perl when you need a real regex feature, a data structure or portability. When a one-liner grows past a line or two, turn it into a script with use strict and use warnings; you have already met the building blocks in Text Processing Idioms.

Habits for safe one-liners

  • Test on a copy or print to the screen before using -i.
  • Quote the code in single quotes on Unix so the shell leaves $, @ and \ alone.
  • Add -w when something behaves strangely; it prints warnings about undefined values.
  • Remember that -l changes print behavior: if you also write \n yourself you will get blank lines.
  • Prefer -lane for column work and -pe for rewrites, as they cover most needs.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the difference between -n and -p?

    Both wrap your code in a loop over input lines. With -p the current line ($_) is printed automatically at the end of each pass; with -n you must call print yourself.
  2. 2. What does perl -lane 'print $F[-1]' file print?

    -a splits each line into @F, and the index -1 refers to the last element. -l strips the newline and adds one back.
  3. 3. Why is it wise to use -i.bak instead of plain -i while learning?

    With an extension, Perl saves the original under that name before writing the changed file, so a mistake in the regex can be undone.
  4. 4. How do you split each line on colons in a one-liner?

    -F sets the split pattern used by the autosplit feature. The pattern is a regex, so -F: splits on colons and puts the pieces in @F.
  5. 5. When is Perl a better choice than sed?

    sed is great for simple substitutions, but Perl offers the full regex engine, hashes and arrays, and a behavior that does not vary between GNU and BSD tools.

Summary

  • -e runs code from the command line; -n and -p loop over input lines in $_, with -p printing automatically.
  • -l handles newlines, -a splits into @F, -F chooses the separator, and -lane is the everyday bundle.
  • BEGIN and END blocks give you setup and totals; $. is the line number.
  • -i.bak edits files in place with a backup; test without -i first.
  • Hashes make one-liners powerful: $seen{$_}++ dedupes, $by{$F[2]}++ counts.
  • Use sed and awk for short jobs, and Perl when you need real regex power, data structures or portability.

Next, make real programs fast in Performance and Profiling.