πŸ“‹ Cheat Sheets

sed & awk Cheat Sheet β€” Text Processing on the Command Line


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.txt

Replace 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 pattern

Delete 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.txt

Remove 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 match

Filter 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.txt

Print 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