Skip to content

RTFM · Tools

sed and awk: the classic pair

Two complementary stream editors: sed rewires text line-wise by substitution; awk treats fields as structured data. Together they replace most ad-hoc scripting temptations.

Saphira Linux dragon mascot

sed: substitute, delete, print selectively

Saphira ships sed supporting extended regular expressions via -E, plus the classic substitution and filtering verbs.

sed repertoire
sed 's/old/new/' file           # first occurrence per line
sed 's/old/new/g' file          # every occurrence
sed -E 's/(\w+)@(\w+)/\2@\1/g'  # extended regex + capture swap
sed -n '/ERROR/p' app.log       # silent mode + explicit print
sed '/^#/d;/^$/d' config        # strip comments and blanks
sed -i.bak 's/a/b/g' file       # in-place with backup safety

-i semantics vary between sed flavours regarding backup suffix handling; pass .bak explicitly or verify behaviour on a scratch copy first.

awk: fields, patterns, arithmetic

awk splits each line on whitespace into $1..$NF; the entire line is $0. That model alone covers most log-reduction tasks.

awk patterns
awk '{print $1}' access.log                    # first column
awk '$9 == 500 {print $7}' access.log          # conditionally filter
awk -F: '{print $1, $3}' /etc/passwd           # colon-delimited
awk '/error/ {count++} END {print count}' app.log  # tally by pattern
awk '{sum+=$2} END {printf "%.1f MB\n", sum/1024}' sizes.txt

The END block runs after input ends — perfect place for totals. BEGIN handles pre-input setup. Between them lies the per-record body, conditionally gated by pattern expressions.

Choosing between them

sed vs awk selection heuristics
Task shapeReach for
Search-replace textual patternsed
Extract/filter columns numericallyawk
Multi-step conditional transformationsawk
Simple global character cleanupsed
Aggregations over delimited dataawk

Learning both pays compound interest: outputs from one feed inputs of the other, chained through pipelines endlessly.

Prove it works: stream editor confidence

a one-liner written from scratch replaces a manual spreadsheet edit on a real log within five minutes.

Did we miss something?

If this page left something unanswered, found an error, or there is another subject you would like documented, tell us. Saphira’s documentation grows from real problems people need to solve.

Send feedback or request a new section →

Prefer not to do it yourself?

Everything needed to do the work yourself is documented here and remains free — we charge for human time, not for withholding knowledge. Sometimes the missing resource is simply time. The same people who build Saphira can provide paid professional help with implementation, migration, troubleshooting and administration.

Ask about professional support →