Perl One-Liners
Learn how to write powerful single-line Perl scripts on the command line using -e, -n, -p, and -i.
Introduction
Beyond full scripts, Perl is famous for its one-liners - complete, useful programs typed directly on the command line. They are a staple of Unix-style text processing, often replacing a chain of grep, sed, and awk with a single readable Perl invocation.
- How to run Perl code directly with perl -e.
- What the -n and -p flags do and how they differ.
- A classic pattern-counting one-liner.
- How to edit files in place safely with -i.
Why Perl One-Liners Are Popular
Perl was designed from the start to make quick text-processing tasks fast to write. A one-liner lets you filter, transform, or summarize a file without opening an editor or writing a saved script - ideal for a quick answer in the middle of a terminal session.
The -e Flag
-e tells perl to execute the code that follows as a string, instead of reading it from a file. This is the foundation every one-liner is built on.
perl -e 'print "Hello, World!\n";'Click Run to see what this code prints.
On Unix shells, wrap one-liners in single quotes as shown above. In Windows PowerShell, single quotes work too, but double-quoted Perl strings inside them may need extra care - test short snippets first.
Automatic Looping with -n
-n wraps your code in an implicit while (<>) { ... } loop, reading input line by line (from files given as arguments, or from standard input) without printing anything automatically. You decide what to print.
perl -ne 'print if /error/i' app.logThis prints only the lines of app.log that contain "error" (case-insensitively) - a quick, dependency-free alternative to grep -i error app.log.
Auto-Print with -p
-p behaves like -n, but automatically prints $_ (the current line) at the end of each loop iteration - perfect for transforming a file line by line, similar to sed.
perl -pe 's/foo/bar/g' input.txt > output.txtEvery occurrence of "foo" on every line becomes "bar", and every line (changed or not) is printed to the redirected output file.
A Classic Line-Counting One-Liner
Combining -n with the special END block gives you a compact way to count how many lines match a pattern, printing the total only once after all lines have been processed.
perl -ne '$count++ if /error/i; END { print "$count\n" }' app.logClick Run to see what this code prints.
In-Place Editing with -i
-i edits the input file directly instead of printing to standard output. Passing a suffix, like -i.bak, tells Perl to keep a backup of the original file before overwriting it - strongly recommended until you trust the substitution.
perl -i.bak -pe 's/localhost/127.0.0.1/g' config.txtThis rewrites config.txt with every "localhost" replaced by "127.0.0.1", while saving the untouched original as config.txt.bak.
More Useful One-Liner Patterns
The -a flag auto-splits each line into the array @F (like awk), and -F sets the field separator. Combined with -l (which handles line endings automatically), this makes column extraction from delimited files a one-liner.
perl -F',' -lane 'print $F[1]' data.csvClick Run to see what this code prints.
Common Mistakes
- Forgetting -n or -p and wondering why nothing prints, or why every line prints twice.
- Using -i without a backup suffix and overwriting the only copy of an important file.
- Mixing up shell quoting rules between Unix shells, cmd, and PowerShell, causing "unexpected" syntax errors.
- Cramming too much logic into a one-liner instead of saving it as a real, readable script once it grows past a line or two.
Best Practices
- Test destructive one-liners (especially -i) on a copy of the file first.
- Always use -i.bak (or similar) until you are confident in the substitution.
- Keep one-liners focused on one transformation; graduate to a script once it gets complex.
- Remember -p auto-prints and -n does not - pick based on whether you want every line echoed back.
Frequently Asked Questions
It splits each input line on whitespace (or the pattern given by -F) into the array @F, similar to awk's automatic field splitting.
It is Perl's default variable - many functions like print and the pattern match operator implicitly operate on it, which is why -n and -p one-liners can be so short.
Yes, from cmd or PowerShell, though quoting conventions differ from Unix shells - PowerShell generally accepts single-quoted Perl one-liners similarly to bash.
Key Takeaways
- perl -e runs Perl code passed directly as a string.
- -n loops over input without auto-printing; -p loops and auto-prints each line.
- END { } blocks run once after all input has been processed - great for totals.
- -i edits files in place; always pair it with a backup suffix at first.
- -a and -F make quick column extraction from delimited files trivial.
Summary
One-liners condense Perl's text-processing strengths into single commands you can run without ever opening an editor. Next, you will look at environment variables - how scripts read configuration and context from the surrounding system.