Click any command to expand the explanation and examples.
βοΈ sed β Stream Editor
s/old/new/ β search and replace sed
# Replace first occurrence per line sed 's/old/new/' file.txtReplace ALL occurrences per line
sed βs/old/new/gβ file.txt
Case insensitive
sed βs/old/new/giβ file.txt
Edit file in place
sed -i βs/old/new/gβ file.txt # Linux sed -i β βs/old/new/gβ file.txt # macOS
Use different delimiter (useful with paths)
sed βs|/usr/local|/opt|gβ file.txt sed βs#http://#https://#gβ file.txt
Print, delete, insert lines sed
# Print specific lines sed -n '5p' file.txt # Line 5 sed -n '5,10p' file.txt # Lines 5-10 sed -n '/pattern/p' file.txt # Lines matching patternDelete lines
sed β5dβ file.txt # Delete line 5 sed β5,10dβ file.txt # Delete lines 5-10 sed β/pattern/dβ file.txt # Delete matching lines sed β/^$/dβ file.txt # Delete empty lines sed β/^#/dβ file.txt # Delete comment lines
Insert / append
sed β3i\New line before 3β file.txt # Insert before line 3 sed β3a\New line after 3β file.txt # Append after line 3 sed β1i\Header lineβ file.txt # Add header
Replace entire line
sed β/pattern/c\Replacement lineβ file.txt
sed one-liners sed
# Remove trailing whitespace sed 's/[[:space:]]*$//' file.txtRemove leading whitespace
sed βs/^[[:space:]]*//β file.txt
Remove blank lines
sed β/^$/dβ file.txt
Print between two patterns
sed -n β/START/,/END/pβ file.txt
Add line numbers
sed = file.txt | sed βN;s/\n/\t/β
Replace only on lines matching a pattern
sed β/error/s/old/new/gβ file.txt
Multiple operations
sed -e βs/foo/bar/gβ -e βs/baz/qux/gβ file.txt
π awk β Column Processing
Print columns awk
# Print specific columns (space-separated by default)
awk '{print $1}' file.txt # First column
awk '{print $2}' file.txt # Second column
awk '{print $NF}' file.txt # Last column
awk '{print $1, $3}' file.txt # First and third
Custom delimiter
awk -Fβ,β β{print $1}β data.csv # Comma
awk -Fβ:β β{print $1}β /etc/passwd # Colon
awk -Fβ\tβ β{print $2}β data.tsv # Tab
Custom output separator
awk -Fβ,β β{print $1, $2}β OFS=β\tβ data.csv
Print entire line
awk β{print $0}β file.txt
awk β{print}β file.txt # Same thing
Filtering rows awk
# Filter by column value awk '$3 > 100' file.txt # Third column > 100 awk '$1 == "Alice"' file.txt # First column is Alice awk '$2 ~ /error/' file.txt # Second column matches regex awk '$2 !~ /test/' file.txt # Doesn't matchFilter by line number
awk βNR == 5β file.txt # Line 5 awk βNR >= 5 && NR <= 10β file.txt # Lines 5-10 awk βNR > 1β file.txt # Skip header
Filter by pattern
awk β/error/β file.txt # Lines containing βerrorβ awk β!/^#/β file.txt # Lines not starting with #
Aggregation and math awk
# Sum a column
awk '{sum += $1} END {print sum}' numbers.txt
Average
awk β{sum += $1; n++} END {print sum/n}β numbers.txt
Count lines
awk βEND {print NR}β file.txt
Count matching lines
awk β/error/ {count++} END {print count}β log.txt
Min / max
awk βNR==1 || $1 > max {max=$1} END {print max}β numbers.txt
Frequency count
awk β{count[$1]++} END {for (k in count) print k, count[k]}β file.txt | sort -rnk2
awk one-liners awk
# Remove duplicate lines (preserving order) awk '!seen[$0]++' file.txtPrint lines longer than 80 chars
awk βlength > 80β file.txt
Swap two columns
awk β{print $2, $1}β file.txt
Add line numbers
awk β{print NR, $0}β file.txt
Join lines with comma
awk β{printf β%s%sβ, sep, $0; sep=β,β} END {print ""}β file.txt
Convert CSV to TSV
awk -Fβ,β β{$1=$1; print}β OFS=β\tβ data.csv
Print between patterns
awk β/START/,/END/β file.txt