Learn grep, sed & awk

The classic Unix trio for searching, editing and summarising text

18 lessons · 4 hours · 4 levels · covers grep, sed & awk GNU grep 3.11, sed 4.9, gawk 5.3

grep finds lines, sed edits them as they stream past and awk splits them into fields and computes with them. Together with sort, uniq, cut and xargs they turn logs, CSVs and config files into answers in one pipeline, on any Unix machine, without writing a program.

Before you start

grep, sed & awk builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics5 lessons · 54 min in total
2. PracticalUse it productively at work, with the usual idioms11 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs15 lessons · 3 hours in total
4. ExpertInternals and the edge cases experts know18 lessons · 4 hours in total

The grep, sed & awk tree

grep, sed & awkText streams and the filter modelText streams and thefilter modelgrep: finding linesgrep: finding linesPatterns in grep: BRE and EREPatterns in grep: BREand EREsed: substitution with s///sed: substitution withs///awk: fields and printingawk: fields andprintinggrep output control and scriptinggrep output controland scriptingsed addresses, ranges and commandssed addresses,ranges and comma…Capture groups and in-place editingCapture groups andin-place editingawk patterns, BEGIN and ENDawk patterns, BEGINand ENDCounting and grouping with awk arraysCounting andgrouping with awk…sort, uniq, cut and trsort, uniq, cut and trPipelinesPipelinesExit status, && and ||Exit status, && and||
Essentials and Practical shown (11 of 18 lessons); the full list is below. Click a lesson to read it.

All 18 lessons, in order

Text streams

  1. Text streams and the filter model · Essentials · 8 min

    Treat text as a stream of lines flowing through small filters: grep selects, sed edits, awk splits into fields and computes.

Searching with grep

  1. grep: finding lines · Essentials · 10 min

    Search files or input for lines matching a pattern, with the everyday flags -i, -v, -n, -c, -w, -l and -r.

  2. Patterns in grep: BRE and ERE · Essentials · 12 min

    Use anchors, character classes and repetition in grep patterns, and know when -E changes which characters are special.

  3. grep output control and scripting · Practical · 10 min

    Show context with -A/-B/-C, stop early with -m, extract matches with -o and use -q with grep's exit status in scripts.

  4. grep -P, ripgrep and smarter searching · Advanced · 10 min

    Use Perl-compatible patterns with grep -P (lookarounds, \d, \K) and know when ripgrep or git grep is the better search tool.

Editing with sed

  1. sed: substitution with s/// · Essentials · 12 min

    Rewrite text on the fly with s/regex/replacement/flags, using g for every match, & for the matched text and other delimiters.

  2. sed addresses, ranges and commands · Practical · 12 min

    Choose which lines a sed command applies to with line numbers, regexes and ranges, and use d, p, -n, i, a and c.

  3. Capture groups and in-place editing · Practical · 12 min

    Reorder text with sed -E capture groups and \1 backreferences, and edit files safely with -i and a backup suffix.

  4. sed: hold space and multi-line editing · Advanced · 15 min

    Work across lines in sed with N, D and P, the hold space (h, H, g, G, x) and labels with branching.

Fields and reports with awk

  1. awk: fields and printing · Essentials · 12 min

    awk splits each line into fields $1..$NF; print the columns you want, change the separator with -F and use NR and NF.

  2. awk patterns, BEGIN and END · Practical · 12 min

    Filter records with conditions and regex matches, and use BEGIN and END blocks to set up and report.

  3. Counting and grouping with awk arrays · Practical · 12 min

    Use awk's associative arrays to count, sum and group by a key, and iterate over the results in END.

  4. awk programs: printf, functions, two files · Advanced · 15 min

    Write real awk programs with printf, string functions, user functions, -v variables, script files and two-file joins via NR==FNR.

Robust pipelines

  1. sort, uniq, cut and tr · Practical · 12 min

    The helper filters that complete most pipelines: sort by keys, count duplicates with uniq -c, cut columns and translate characters.

  2. Safe filenames, xargs and locale · Advanced · 15 min

    Handle any filename with NUL separators (find -print0, grep -Z, xargs -0), and control behaviour and speed with LC_ALL.

Under the hood

  1. awk semantics: strings, numbers and $0 · Expert · 15 min

    Understand awk's dual string/number values, how assigning a field rebuilds $0 with OFS, and how RS, FS and getline really behave.

  2. How grep is fast, and when it isn't · Expert · 12 min

    Why grep beats a naive loop: literal-string skipping, DFA matching, mmap-free buffered reads, and what slows it down (backreferences, -P, locales).

  3. POSIX, GNU and BSD differences · Expert · 12 min

    Know which grep, sed and awk features are POSIX and which are GNU or BSD extensions, so your scripts run on Linux, macOS and minimal containers.