PERL ONE-LINERS - THE BASICS
Why Perl One-Liners?
Perl was designed for text processing. One-liners let you transform text without writing a script file. They're perfect for:
- Quick find-and-replace
- Extracting data from files
- Reformatting text on the fly
- Piping between commands
Same regex syntax as ack and rename. Learn once, use everywhere.
The Basic Structure
perl [options] 'code' [files]
Most common options:
-e Execute code from command line
-n Loop over input lines (don't print by default)
-p Loop over input lines (print by default)
-i Edit files in place
-l Automatic newline handling
The -e flag is almost always used. It says "here comes the code."
Hello World
echo "hello world" | perl -pe 's/world/universe/'
Output: hello universe
The -p flag loops over input and prints each line. The s/// does our substitution.
Processing Files
You can give perl files directly:
perl -pe 's/old/new/g' file.txt
Or pipe from another command:
cat file.txt | perl -pe 's/old/new/g'
Both do the same thing.
The -n vs -p Difference
-p Read lines, run code, print result (most common)
-n Read lines, run code, don't print automatically
With -n you control what gets printed:
perl -ne 'print if /pattern/' file.txt
This prints only lines matching the pattern (like grep).
In-Place Editing
The -i flag modifies files directly:
perl -i -pe 's/old/new/g' file.txt
WARNING: This changes the file! No undo!
Safer: create a backup:
perl -i.bak -pe 's/old/new/g' file.txt
This saves the original as file.txt.bak before modifying.
Basic Substitution
Find and replace across a file:
perl -pe 's/foo/bar/g' input.txt > output.txt
Breaking it down:
-p Print each line after processing
-e Code follows
s/foo/bar/ Replace foo with bar
/g Global (all occurrences)
Case-Insensitive Replacement
perl -pe 's/error/warning/gi' log.txt
The /i makes it case-insensitive. The /g makes it global.
Print Matching Lines Only
Like grep, but with Perl regex:
perl -ne 'print if /pattern/' file.txt
Or the inverse (non-matching lines):
perl -ne 'print unless /pattern/' file.txt
Extract Just the Match
Print only what the regex matched:
perl -ne 'print "$1\n" if /(\d+)/' file.txt
This prints captured numbers, one per line.
Without capture group, use $& for the whole match:
perl -ne 'print "$&\n" if /\d+/' file.txt
Multiple Files
Perl processes multiple files in sequence:
perl -pe 's/old/new/g' file1.txt file2.txt file3.txt
With -i, modifies all of them:
perl -i -pe 's/old/new/g' *.txt
The Default Variable $_
In Perl one-liners, $_ is the "current line." Most operations work on $_ by default:
perl -ne 'print if /pattern/'
Is shorthand for:
perl -ne 'print $_ if $_ =~ /pattern/'
You rarely need to write $_ explicitly.
Modifying and Printing
Transform each line:
perl -pe '$_ = uc' # Uppercase everything
perl -pe '$_ = lc' # Lowercase everything
perl -pe '$_ = reverse' # Reverse each line
Or use substitution:
perl -pe 's/^\s+//' # Remove leading whitespace
perl -pe 's/\s+$//' # Remove trailing whitespace
perl -pe 's/^\s+|\s+$//g' # Trim both ends
Numbering Lines
Add line numbers:
perl -pe '$_ = "$. $_"' file.txt
$. is the current line number. Output:
1 First line
2 Second line
3 Third line
Quick Field Extraction
Split lines on whitespace and print specific fields:
perl -ane 'print "$F[0]\n"' file.txt
The -a flag auto-splits into @F array. Fields are zero-indexed.
Print first and third fields:
perl -ane 'print "$F[0] $F[2]\n"' file.txt
Custom Field Separator
Split on commas (CSV):
perl -F',' -ane 'print "$F[0]\n"' data.csv
The -F flag sets the field separator.
Sum Numbers
Add up all numbers in a file:
perl -ne '$sum += $_; END { print "$sum\n" }' numbers.txt
The END block runs after all lines are processed.