Sed & Awk Text Processing Commands Matrix
The complete Sed and Awk command cheat sheet for Linux sysadmins and DevOps: regex replacement (s///), in-place editing (-i), line deletions, Awk column printing ($1, $NF), delimiter parsing (-F), and frequency counts with 1-click copy.
Sed Stream Editing (7 commands)
sed 's/foo/bar/' file.txt
Replaces the first occurrence of "foo" with "bar" on each line and prints to stdout.
sed 's/foo/bar/g' file.txt
Replaces all occurrences (global flag /g) of "foo" with "bar" on every line.
sed -i.bak 's/localhost/127.0.0.1/g' config.env
Edits the file in-place (-i) while safely creating an unedited backup copy (config.env.bak).
sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3.\2.\1/g' dates.txt
Uses extended regular expressions (-E) to capture date groups and reformats YYYY-MM-DD to DD.MM.YYYY.
sed '/^#/d; /^$/d' nginx.conf
Deletes (/d) all comment lines starting with # and all empty lines, streaming clean configuration.
sed -n '10,25p' access.log
Suppresses automatic line printing (-n) and prints exclusively line numbers 10 through 25.
sed -n '/ERROR/,/END_TRACE/p' app.log
Prints all lines between the opening pattern /ERROR/ and closing pattern /END_TRACE/ inclusive.
Awk Field Processing (8 commands)
awk '{print $1, $NF}' access.log
Extracts and prints the first field ($1, usually IP) and the last field ($NF) of each line.
awk -F':' '{print $1, $3, $6}' /etc/passwd
Sets custom field delimiter to colon (-F':') and prints username ($1), UID ($3), and home directory ($6).
awk -F',' '$3 > 500 {print $1, $3}' billing.csv
Evaluates numeric condition on column 3 and outputs rows where invoice amount exceeds $500.
awk '{sum += $5} END {print "Total:", sum, "bytes"}' access.log
Accumulates byte sizes from column 5 into variable sum and prints the grand total at EOF (END block).
awk '{count[$1]++} END {for (ip in count) print count[ip], ip}' access.log | sort -nr | head -10
Builds an associative frequency hash table of IP addresses and outputs the top 10 most frequent visitors.
awk 'NF > 0' raw_data.txt
Filters out blank or whitespace-only lines (NF = Number of Fields must be greater than zero).
awk 'NR==FNR {a[$1]=$2; next} $1 in a {print $1, a[$1], $2}' file1.txt file2.txt
Performs an in-memory inner join between two tabular files based on the primary key in column 1.
awk '{printf "%-20s | %10.2f MB\n", $1, $2 / 1024 / 1024}' disk.txt
Formats structured tabular output using C-style printf with left-aligned strings and fixed decimal floats.