Code
perl
# Replace text in-place
perl -i -pe 's/foo/bar/g' file.txt
# Print lines matching pattern
perl -ne 'print if /pattern/' file.txt
# Sum numbers (one per line)
perl -nle '$sum += $_; END { print $sum }' nums.txt
# Field sum (CSV: sum of column 2)
perl -F, -nle '$sum += $F[1]; END { print $sum }' data.csv
# Word frequency
perl -nle '$count{$_}++ for split' file.txt | sort
# Reverse lines
perl -e 'print reverse <>' file.txt
# Print lines 5-10
perl -ne 'print if 5..10' file.txt
# Grep with context (lines before/after)
perl -ne 'print if /pattern/../end/' file.txt
# Trim whitespace
perl -pe 's/^\s+|\s+$//g' file.txt
# CamelCase to snake_case
perl -pe 's/([a-z])([A-Z])/$1_$2/g; $_ = lc' file.txt
# Common flags:
# -e: execute code
# -n: wrap in while (<>) { ... }
# -p: like -n but prints $_ automatically
# -l: auto chomp, adds newline to print
# -i: in-place edit
# -F: set field separator (auto-split into @F)