Why Conditionals Matter in AWK
AWK is fundamentally a pattern-action language: every rule has the shape pattern { action }, and the pattern itself is a condition. When the pattern evaluates to true (non-zero or non-empty), AWK runs the action for that record. This means conditionals are woven into AWK at two levels — the top-level pattern that gates whole rules, and the if/else statements used inside actions for finer control. Understanding both is the key to writing filters that select exactly the lines you want.
Cricket analogy: A pattern in AWK is like the third umpire reviewing each delivery: only when the condition 'ball pitched in line and hitting the stumps' is true does the LBW action — raising the finger — get executed.
Comparison and Pattern Conditions
The simplest conditionals are comparison expressions used directly as patterns: $3 > 100, $1 == "admin", or NR > 1 to skip a header line. AWK supports the full set of relational operators (<, <=, ==, !=, >=, >) plus the regex-match operators ~ and !~ for testing whether a field matches a pattern, such as $2 ~ /error/. A subtle but important detail is AWK's dynamic typing: a field is treated as a number when compared numerically and as a string otherwise, so $1 == 10 and $1 == "10" can behave differently depending on the field's content.
Cricket analogy: Comparison operators are like a selector's cutoffs: pick a batter only if average > 45 and strike-rate > 130 — each relational test narrows the squad the way $3 > 100 narrows records.
The if / else if / else Statement
Inside an action block you use if (condition) statement with optional else if and else branches, just like C. Braces group multiple statements, and you can nest conditionals freely. AWK also offers the ternary operator condition ? valueIfTrue : valueIfFalse, which is ideal for compact assignments such as grade = (score >= 60) ? "pass" : "fail". Because AWK treats zero, an empty string, and an uninitialized variable as false and everything else as true, you can write terse tests like if (count) to mean 'if count is non-zero'.
Cricket analogy: An if/else-if ladder is like a captain reading the pitch: if it's turning, bowl the spinner; else if there's swing, bowl the seamer; else keep the part-timer — one branch per condition.
# grade a scores file: name TAB score
awk -F'\t' 'NR > 1 {
if ($2 >= 90) grade = "A"
else if ($2 >= 75) grade = "B"
else if ($2 >= 60) grade = "C"
else grade = "F"
status = ($2 >= 60) ? "pass" : "fail"
printf "%-12s %3d %s (%s)\n", $1, $2, grade, status
}' scores.tsv
# pattern-as-condition: print only rows where column 3 matches a pattern
awk '$3 ~ /^ERROR/ && $4 > 500 { print $0 }' app.logThe pattern in a rule and an if inside the action are interchangeable for a single test: $3 > 100 { print } is equivalent to { if ($3 > 100) print }. Prefer the pattern form for simple filters — it is shorter and reads like a specification of which records you want.
Beware the difference between = (assignment) and == (comparison). Writing if ($1 = 0) assigns 0 to $1 and the condition becomes false every time, silently changing your data. AWK will not warn you, so always double-check equality tests use ==.
- Every AWK rule is
pattern { action }, and the pattern is itself a condition that gates the action. - Comparison operators (
<,<=,==,!=,>=,>) and regex-match operators (~,!~) build pattern conditions. - Inside actions, use
if / else if / elsewith braces for multi-statement branches, just like C. - The ternary operator
cond ? a : bgives compact value selection, e.g.status = (n>0) ? "ok" : "empty". - AWK treats 0, empty string, and uninitialized variables as false; all other values are true.
- Use
==for comparison — accidentally using=performs an assignment and corrupts your test. - Dynamic typing means a field may be compared as a number or a string depending on its content and context.
Practice what you learned
1. In AWK, what does the rule `$3 > 100 { print }` do?
2. Which operator tests whether field 2 matches the regular expression /warn/?
3. What value does `x = (0) ? "a" : "b"` assign to x?
4. Why is `if ($1 = 5)` almost always a bug?
5. Which values does AWK consider false in a conditional?
Was this page helpful?
You May Also Like
Loops in AWK
Master AWK's iteration constructs — while, do-while, for, and the array-oriented for-in loop — along with break, continue, and next for controlling flow through records and data.
Arrays in AWK
Understand AWK's associative arrays — string-keyed, dynamically growing collections used for counting, grouping, and lookups — plus multi-dimensional emulation, membership tests, and deletion.
String Functions in AWK
Explore AWK's built-in string functions — length, substr, index, split, sub, gsub, match, sprintf, and case conversion — for extracting, searching, and transforming text.
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