Skip to main content
RunBook Academy

LinuxII · Shell and Command-Line OperationsText tools

sed and awk in production

Intermediate⏱ ~12 minbashsedawkgawk

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

Not yet marked complete on this device.

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.

Read-only / Safesed substitute
$ echo 'Hello World' | sed 's/World/Linux/'
Hello Linux

Illustrative output

Read-only / Safesed delete
$ printf 'a\nb\nc\n' | sed '2d'
a
c

Illustrative output

Read-only / Safesed range extraction
$ cat /etc/ssh/sshd_config | sed -n '/^Port/,/^$/p' | head -5
Port 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).

Read-only / Safeawk UID 0 audit
$ awk -F: '$3 == 0 { print "UID 0 account:", $1 }' /etc/passwd
UID 0 account: root

Illustrative output

Read-only / Safeawk frequency count
$ awk '{ counts[$1]++ } END { for (u in counts) printf "%d %s
", counts[u], u }' /etc/passwd | sort -rn | head -5
46 /
1 /usr/sbin
1 /bin/sync
1 /bin/shutdown
1 /sbin

Illustrative output

Read-only / Safeawk numeric range
$ awk -F: '$3 >= 1000 && $3 < 65534 { print $1, $3 }' /etc/passwd | head
nobody 65534
ssm-user 1001
deploy 1002

Illustrative output

awk vs the basic toolkit

When to reach for awk instead of grep + cut + sed:

TaskReach for
Filter lines by contentgrep
Extract a fixed column from delimited textcut
Substitute on a per-line basissed
Multiple delimiters, escaped fields, or field-based decisionsawk
Per-record arithmetic (sums, averages, percentiles)awk
Per-record printing driven by previous recordsawk
Pretty-printing structured outputawk

A real production example: nginx access log triage

The combination of grep + awk + sort + uniq is the canonical nginx/Apache access-log diagnostic.

Read-only / Safetop 5xx URLs
$ awk '$9 ~ /^5/ { print $7 }' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head
...

Illustrative output

Knowledge check

Knowledge check · 3 questions

  1. Q1. What does `sed -n '/start/,/end/p' file` do?

  2. Q2. awk treats every whitespace-separated word in a line as a separate field by default.

  3. Q3. Which of the following are appropriate awk tasks? Select all that apply.

Passing score: 75%. Answers are checked in this browser.