Overview
Reach for awk when the task needs fields and for sed when it needs line edits. This card holds the expressions for both, plus the portability traps that bite when a script moves between Linux and macOS. For find, grep, and pipelines that chain all four tools, see find-grep-awk-sed.
awk
awk is a field-oriented language; use it for columnar data.
| Expression | What it does |
|---|---|
awk '{print $1}' file | Print first field (default delimiter: whitespace). |
awk '{print $NF}' file | Print last field. |
awk -F: '{print $1}' /etc/passwd | Set field delimiter to :. |
awk 'NR==5' file | Print line 5. |
awk 'NR>=3 && NR<=7' file | Print lines 3 to 7. |
awk '/pattern/' file | Print lines matching a regex. |
awk '!/pattern/' file | Print lines not matching. |
awk '{sum += $2} END {print sum}' file | Sum column 2. |
awk 'BEGIN {FS=","} {print $1, $3}' file.csv | CSV with explicit FS. |
awk '{print $2, $1}' file | Swap columns 1 and 2. |
awk 'length($0) > 80' file | Lines longer than 80 characters. |
awk '!seen[$0]++' file | Remove duplicate lines (preserving order). |
awk '{gsub(/old/, "new"); print}' file | Global substitution in each line. |
awk is ideal for log parsing and CSV reshaping when you need field access rather than full regex substitution.
sed
sed edits streams line by line; use it for substitution, deletion, and insertion.
| Expression | What it does |
|---|---|
sed 's/old/new/' file | Replace first occurrence per line. |
sed 's/old/new/g' file | Replace all occurrences per line. |
sed -i 's/old/new/g' file | In-place edit (GNU sed; Linux). |
sed -i '' 's/old/new/g' file | In-place edit (BSD sed; macOS). |
sed -i.bak 's/old/new/g' file | In-place with backup; portable. |
sed '/pattern/d' file | Delete lines matching a pattern. |
sed -n '/start/,/end/p' file | Print lines between two patterns. |
sed -n '10,20p' file | Print lines 10 to 20. |
sed '1d' file | Delete the first line (header removal). |
sed '$d' file | Delete the last line. |
sed 's/^/PREFIX: /' file | Prepend to every line. |
sed 's/$/ SUFFIX/' file | Append to every line. |
sed '/pattern/a\new line' file | Append a line after each match (GNU sed). |
Common gotchas
sed -iis GNU;sed -i ''is BSD. Usesed -i.bakfor portable in-place editing; then remove*.bakwithfind . -name '*.bak' -delete.awk '{print $1}'uses whitespace as the delimiter. A CSV with spaces inside quoted fields will be split incorrectly. Set-F","and handle quotes explicitly or use a proper CSV tool.sed 's/./X/g'replaces every character;.is a regex metacharacter. Escape it as\.to match a literal dot.