grep vs sed vs awk on Linux: Text Processing Comparison

grep sed awk linux - custom-textproc-featured.png

The classic pipeline is grep → sed → awk, but you do not need all three. grep filters lines; sed edits streams; awk treats fields and can sum or group — the comparison every junior admin searches before writing a ten-line script.

More: grep,
awk & sed,
cut.

grep — keep matching lines

Regex, case, context lines — first tool for ERROR in a log.

bashgrep
grep -E 'error|crit' /var/log/syslog | tail
zgrep 'POST /api' access.log.gz | head
grep -E
filter

sed — transform text in flight

Substitute, delete, print range — often one-liners without a full script.

bashsed
sed -n '1,20p' file.conf
sed 's/http:/https:/g' urls.txt | head
sed
substitute

awk — fields and reports

Default FS whitespace; perfect for df, ss, or Apache combined logs.

bashawk
df -hP | awk 'NR>1 {print $1, $5}'
awk -F: '$3 == 0 {print $1}' /etc/group
awk
columns

When to combine vs pick one

Filter first (grep), normalize (sed), aggregate last (awk). For JSON logs, consider jq instead.

NoteAvoid grep | sed | awk on gigabyte files without narrowing first — use tail, time range, or indexed logs.
pipeline
compare

Quick Reference

  • Match lines: grep
  • Edit stream: sed
  • Columns / math: awk

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.