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.
# 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
1. What is the default action when a pattern has no action block?
2. When does a BEGIN block execute?
3. What does the pattern /START/,/END/ match?
4. What does an action with no pattern do?
5. Which built-in variable gives the number of fields on the current line?
Was this page helpful?
You May Also Like
What Is AWK?
AWK is a compact, data-driven programming language built for scanning text files line by line and reacting to patterns. It excels at extracting columns, computing summaries, and reshaping structured text.
Fields and Records in AWK
AWK splits input into records (lines) and fields (columns). Mastering $1..$NF, the NR/NF variables, and the FS/RS/OFS/ORS separators unlocks nearly all column-oriented text work.
Your First AWK One-Liner
Walk through building practical AWK one-liners step by step: printing columns, filtering rows, summing fields, and counting lines. These small programs cover a huge share of everyday text tasks.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics