AWK Patterns

Chapter 3
Patterns and Conditions
Regex patterns, comparison expressions, ranges, and the BEGIN/END blocks introduced in Chapter 2

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 pattern
A regular expression between slashes — matches if it's found anywhere in the current line (or a specific field, if explicitly targeted).
/^ERROR/ { print }
🔢 Comparison expression
Any expression that evaluates to true/false — comparing fields, NR, NF, or computed values.
NF > 5 { print }
↔️ Range pattern
Two patterns separated by a comma — matches every line from the first match through the second match, inclusive.
/START/,/END/ { print }
🎬 BEGIN / END
Not tied to any line at all — BEGIN runs once before any input is read; END runs once after all input is processed.
BEGIN { print "Starting..." }

Regex Patterns in Depth

$ # Anchors work exactly as in grep/sed regex
$ 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
/pattern/ alone checks the whole line; field ~ /pattern/ checks one specific field
A bare /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

PatternMatches lines where...
NF > 3The line has more than 3 fields
$1 == "admin"Field 1 is exactly "admin"
$4 >= 100Field 4, treated as a number, is 100 or more
NR % 2 == 0The line number is even (every other line)
$2 != "" Field 2 is not empty
$ # Print only lines where field 4 (e.g. a price) exceeds 100
$ 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.

$ # Extract everything between two markers, inclusive
$ awk '/^\[database\]/,/^\[\/database\]/' config.ini
A range pattern always completes once started — even if the end marker never appears
If the second pattern in a range never matches for the rest of the file, AWK keeps the range "open" and matches every remaining line through to the end of input. Worth double-checking the end marker genuinely exists if a range pattern seems to be matching far more than expected.

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.

BEGIN { } runs once, first per-line rules runs once PER LINE END { } runs once, last
BEGIN and END are the only two phases NOT tied to a specific line — everything else runs once per line of input
$ # A simple line counter using BEGIN and END together
$ 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