100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

AWK Pattern-Action Syntax

Every AWK program is a sequence of pattern { action } rules. Understanding how patterns select lines and actions transform them is the core mental model of the whole language.

FoundationsBeginner9 min readJul 10, 2026
Analogies

The pattern { action } Rule

An AWK program is built from one or more rules, each written as an optional pattern followed by an optional action in braces. For every input line, AWK checks each rule in order: if the pattern matches, it runs the action. If you supply a pattern with no action, AWK uses the default action of printing the whole line ($0). If you supply an action with no pattern, that action runs on every line. This pairing of a condition with a consequence is the entire grammar of the language.

🏏

Cricket analogy: A pattern-action rule is like a fielding plan: 'if the ball goes to third man (pattern), the fielder throws to the keeper (action)'; no condition met, no action taken.

Kinds of Patterns

Patterns come in several flavours. A regular expression enclosed in slashes, like /ERROR/, matches lines containing that text. A relational or boolean expression, like $3 > 100 or NR % 2 == 0, matches when the expression is true. The special patterns BEGIN and END match before any input is read and after all input is exhausted, respectively, making them ideal for setup and reporting. You can also use a range pattern, /start/,/stop/, which matches every line from one match through the next. Comma-separated ranges are inclusive of both endpoints.

🏏

Cricket analogy: The BEGIN and END blocks are like the national anthem before play and the presentation ceremony after: they happen outside the innings themselves, bookending the match.

BEGIN, END, and Accumulation

A very common structure uses BEGIN to initialize variables, the main body to accumulate values as lines stream by, and END to print a final result. For example, to sum a column you keep a running total in the body and print it once in END. AWK variables spring into existence with a value of zero or the empty string, so you often do not even need the BEGIN block for simple counters. This BEGIN-body-END shape is the backbone of most real AWK reports.

🏏

Cricket analogy: BEGIN-body-END is like a match scorecard: set up the sheet before play, tally runs ball by ball through the innings, then announce the final total at stumps.

awk
# Print only lines containing ERROR (regex pattern, default print action)
/ERROR/

# Print lines where the third field exceeds 100 (relational pattern)
$3 > 100 { print $0 }

# BEGIN-body-END: sum the second column and report the total
BEGIN { total = 0 }
      { total += $2 }
END   { print "Total:", total }

# Range pattern: print everything between START and END markers, inclusive
/START/,/END/

The built-in variable NR holds the current record (line) number, and NF holds the number of fields on the current line. Patterns like NR == 1 (skip a header by pairing with next) or NF == 0 (blank lines) are extremely common building blocks.

  • An AWK program is a list of rules, each an optional pattern and an optional action in braces.
  • A pattern with no action prints $0; an action with no pattern runs on every line.
  • Patterns can be regexes (/ERROR/), expressions ($3 > 100), ranges (/a/,/b/), or BEGIN/END.
  • BEGIN runs before input, END runs after all input, ideal for setup and reporting.
  • The BEGIN-body-END shape underlies most accumulation reports like column sums.
  • Uninitialized variables start at 0 or the empty string, simplifying counters.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#AWKStudyNotes#AWKPatternActionSyntax#AWK#Pattern#Action#Syntax#StudyNotes#SkillVeris#ExamPrep