AWK Patterns
Chapter 1 introduced /ERROR/ as a pattern without explaining what makes something a valid pattern at all. This chapter covers the full range of pattern types AWK supports — regex, comparisons, ranges — plus the two special patterns, BEGIN and END, that don't match lines at all but control setup and teardown.
The Pattern Types
Regex Patterns in Depth
$ awk '/^ERROR/' app.log # lines starting with ERROR
$ awk '/\.log$/' filelist.txt # lines ending in .log
$ # Matching against a SPECIFIC field, not the whole line — use ~
$ awk '$3 ~ /^admin/' users.txt
$ # Negated match with !~
$ awk '$3 !~ /^admin/' users.txt
/ERROR/ pattern matches if ERROR appears anywhere in the line, including in a field you don't actually care about. $3 ~ /ERROR/ is far more precise — it only matches if field 3 specifically contains it, ignoring the rest of the line entirely.
Comparison Expressions
| Pattern | Matches lines where... |
|---|---|
| NF > 3 | The line has more than 3 fields |
| $1 == "admin" | Field 1 is exactly "admin" |
| $4 >= 100 | Field 4, treated as a number, is 100 or more |
| NR % 2 == 0 | The line number is even (every other line) |
| $2 != "" | Field 2 is not empty |
$ awk '$4 > 100' sales.csv
$ # Combine conditions with && (and) / || (or)
$ awk '$4 > 100 && $2 == "active"' sales.csv
Range Patterns
A range pattern matches every line starting from where the first sub-pattern matches, through the line where the second sub-pattern matches — genuinely useful for extracting a section out of a larger structured file.
$ awk '/^\[database\]/,/^\[\/database\]/' config.ini
BEGIN and END — Setup and Teardown
Chapter 2 used a BEGIN block to set FS/OFS before any line was processed. The full picture: AWK's execution has three distinct phases, and BEGIN/END are how you hook into the first and last.
$ awk '
BEGIN { print "Starting scan..." }
/ERROR/ { count++ }
END { print "Found " count " errors" }
' app.log
That count variable persists across every line because AWK variables aren't scoped to a single pattern-action block — they live for the entire run, which is exactly what makes accumulating totals across BEGIN → per-line → END possible. Chapter 6 covers variables and arrays in full depth.
Chapter 3 Quick Reference
- /regex/ — matches if found anywhere in the line; field ~ /regex/ — matches only within that field
- !~ — negated match; combine conditions with && / ||
- Comparison patterns — NF, NR, $N compared with ==, !=, >, <, etc.
- /start/,/end/ — range pattern; matches from the first match through the second, inclusive
- A range with no matching end stays open through the rest of the input — verify the end marker exists
- BEGIN { } — runs once before any line; END { } — runs once after all lines
- Variables persist across the whole run — the foundation of counting/accumulating patterns covered fully in Chapter 6
- Next chapter: built-in variables and functions — string functions, math functions