---
title: "awk and sed Cheatsheet"
slug: "awk-sed"
category: "cheatsheets"
tags: ["cheatsheets", "bash", "awk", "sed", "cli", "text-processing", "reference"]
status: "stable"
last_updated: 2026-09-27
summary: "awk field expressions and sed stream edits for columnar data, substitution, deletion, and line ranges, with GNU vs BSD in-place gotchas."
related:
  [
    "[[cheatsheets/find-grep-awk-sed]]",
    "[[cheatsheets/bash-one-liners]]",
    "[[cheatsheets/regex-patterns]]",
    "[[cheatsheets/regex-flags]]",
    "[[coding/shell]]",
    "[[cheatsheets/index|Cheatsheets]]",
  ]
---

## 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 [[cheatsheets/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 -i` is GNU; `sed -i ''` is BSD. Use `sed -i.bak` for portable in-place editing; then remove `*.bak` with `find . -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.

## Related

- [[cheatsheets/find-grep-awk-sed]]
- [[cheatsheets/bash-one-liners]]
- [[cheatsheets/regex-patterns]]
- [[cheatsheets/regex-flags]]
- [[coding/shell]]
- [[cheatsheets/index|Cheatsheets]]
