LinuxII · Shell and Command-Line OperationsText tools
sed and awk in production
What you'll learn
- Use sed for line-level substitution, deletion, and extraction
- Use awk for field-based extraction and arithmetic
- Recognise when awk is the right tool over cut + grep + sed
- Avoid common sed/awk pitfalls in production scripts
Prerequisites
Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09
sed and awk are full programming languages specialised for stream
text processing. Together with grep they cover virtually every
ad-hoc log-triage task a sysadmin faces.
sed — line-oriented transformations
The basic sed command:
sed [-n] 'address{command}' file
- address — which lines to act on (1, 5, 10,20, /regex/, $).
- command — what to do (s substitute, d delete, p print, i insert, a append, c change, y transliterate).
The most common production pattern: substitution.
$ echo 'Hello World' | sed 's/World/Linux/'Hello LinuxIllustrative output
$ printf 'a\nb\nc\n' | sed '2d'a
cIllustrative output
$ cat /etc/ssh/sshd_config | sed -n '/^Port/,/^$/p' | head -5Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::Illustrative output
awk — field-oriented programming
awk reads input as records (lines by default) split into fields. A
program is a series of pattern { action } clauses:
awk 'condition { action }' file
If condition matches, action runs. $1, $2, … are the fields;
$0 is the whole line; NF is the field count; NR is the record
number; FS is the field separator (default: whitespace).
$ awk -F: '$3 == 0 { print "UID 0 account:", $1 }' /etc/passwdUID 0 account: rootIllustrative output
$ awk '{ counts[$1]++ } END { for (u in counts) printf "%d %s
", counts[u], u }' /etc/passwd | sort -rn | head -546 /
1 /usr/sbin
1 /bin/sync
1 /bin/shutdown
1 /sbinIllustrative output
$ awk -F: '$3 >= 1000 && $3 < 65534 { print $1, $3 }' /etc/passwd | headnobody 65534
ssm-user 1001
deploy 1002Illustrative output
awk vs the basic toolkit
When to reach for awk instead of grep + cut + sed:
| Task | Reach for |
|---|---|
| Filter lines by content | grep |
| Extract a fixed column from delimited text | cut |
| Substitute on a per-line basis | sed |
| Multiple delimiters, escaped fields, or field-based decisions | awk |
| Per-record arithmetic (sums, averages, percentiles) | awk |
| Per-record printing driven by previous records | awk |
| Pretty-printing structured output | awk |
A real production example: nginx access log triage
The combination of grep + awk + sort + uniq is the canonical nginx/Apache access-log diagnostic.
$ awk '$9 ~ /^5/ { print $7 }' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head...Illustrative output
Knowledge check
Knowledge check · 3 questions
Q1. What does `sed -n '/start/,/end/p' file` do?
Q2. awk treats every whitespace-separated word in a line as a separate field by default.
Q3. Which of the following are appropriate awk tasks? Select all that apply.
Passing score: 75%. Answers are checked in this browser.