PERL ONE-LINERS - REAL WORLD EXAMPLES
These are battle-tested patterns for common tasks.
Log File Processing
Extract timestamps from logs:
perl -ne 'print "$1\n" if /(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/' app.log
Count lines containing "error":
perl -ne '$c++ if /error/i; END { print "$c\n" }' app.log
Extract unique IP addresses:
perl -ne 'print "$1\n" if /(\d+\.\d+\.\d+\.\d+)/' access.log | sort -u
CSV Processing
Print second column of a CSV:
perl -F',' -ane 'print "$F[1]\n"' data.csv
Skip the header line:
perl -F',' -ane 'print "$F[1]\n" if $. > 1' data.csv
Reorder columns (print columns 3, 1, 2):
perl -F',' -ane 'print "$F[2],$F[0],$F[1]\n"' data.csv
JSON Wrangling
Extract a specific field (crude but works):
perl -ne 'print "$1\n" if /"name":\s*"([^"]+)"/' data.json
For serious JSON work, use jq. But for quick extracts, this works.
Bulk Text Transformation
Convert Windows line endings to Unix:
perl -i -pe 's/\r\n/\n/' *.txt
Convert Unix to Windows:
perl -i -pe 's/\n/\r\n/' *.txt
Remove blank lines:
perl -ne 'print unless /^$/' file.txt
Collapse multiple blank lines to one:
perl -00 -pe '' file.txt
The -00 flag enables "paragraph mode."
Working with Base64
This comes up constantly with AI APIs.
Remove newlines from base64:
cat image.b64 | perl -pe 's/\s//g'
Add newlines every 76 characters (standard format):
perl -pe 's/(.{76})/$1\n/g' raw.b64
Extracting Data from AI Responses
Given a response like:
NUDE_CHEST: NO
NUDE_LOWER: NO
WEAPON: YES
CONFIDENCE: 0.87
Extract the WEAPON value:
perl -ne 'print "$1\n" if /WEAPON:\s*(\w+)/' response.txt
Extract all YES values:
perl -ne 'print "$1\n" if /(\w+):\s*YES/' response.txt
Parse confidence as a number:
perl -ne 'print "$1\n" if /CONFIDENCE:\s*(\d+\.\d+)/' response.txt
Batch File Content Replacement
Fix a typo across all files:
perl -i -pe 's/teh/the/g' *.txt
Update a version number:
perl -i -pe 's/version: 1\.0/version: 1.1/' config.yml
Comment out lines matching a pattern:
perl -i -pe 's/^(.*DEBUG.*)$/# $1/' code.py
Reformatting Data
Convert space-separated to CSV:
perl -pe 's/\s+/,/g' data.txt
Convert tabs to spaces:
perl -pe 's/\t/ /g' file.txt
Remove duplicate spaces:
perl -pe 's/ +/ /g' file.txt
Extracting URLs
Pull all URLs from a file:
perl -ne 'print "$1\n" while /(https?:\/\/[^\s"<>]+)/g' page.html
The while loop finds all matches, not just the first.
Extracting Email Addresses
perl -ne 'print "$1\n" while /([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g' file.txt
Processing stdin from Other Commands
Combine with curl:
curl -s https://example.com | perl -ne 'print "$1\n" if /<title>([^<]+)</'
Combine with find:
find . -name "*.log" | perl -ne 'chomp; print "Found: $_\n"'
Combine with ack:
ack -o "\d+" | perl -ne '$sum += $_; END { print "Total: $sum\n" }'
Multi-line Processing
Read entire file as one string:
perl -0777 -pe 's/old/new/g' file.txt
The -0777 flag slurps the whole file. Useful for patterns spanning lines.
Remove C-style comments:
perl -0777 -pe 's|/\*.*?\*/||gs' code.c
BEGIN and END Blocks
Set up variables before processing:
perl -ne 'BEGIN { $sum = 0 } $sum += $_; END { print $sum }' nums.txt
Print headers:
perl -pe 'BEGIN { print "=== OUTPUT ===\n" }' file.txt
The -l Flag
Automatically chomp input and add newlines to output:
perl -lne 'print uc' file.txt
This is cleaner than manually handling newlines.
Debugging One-Liners
Add print statements to see what's happening:
perl -ne 'print "LINE: $_"; print "MATCH: $1\n" if /(\d+)/' file.txt
Use Data::Dumper for complex data:
perl -MData::Dumper -ne '@F = split /,/; print Dumper(\@F)' data.csv
Converting to a Script
When a one-liner gets too complex, make it a script:
#!/usr/bin/env perl
use strict;
use warnings;
while (<>) {
s/old/new/g;
print;
}
Save as script.pl, chmod +x, and run on any file.
Speed Tips
For huge files, Perl one-liners are fast. But:
1. Avoid capturing groups if you don't need them 2. Use index() for literal string matching 3. Pre-compile regex with qr// for repeated matches
Example with qr//:
perl -ne 'BEGIN { $re = qr/pattern/ } print if /$re/' huge.txt