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

Conditionals in AWK

Learn how AWK makes decisions using patterns, if/else statements, the ternary operator, and comparison and logical operators to selectively process input records.

Control Flow & FunctionsBeginner9 min readJul 10, 2026
Analogies

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.

awk
# 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.log

The 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 / else with braces for multi-statement branches, just like C.
  • The ternary operator cond ? a : b gives 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

Was this page helpful?

Topics covered

#Programming#AWKStudyNotes#ConditionalsInAWK#Conditionals#AWK#Matter#Comparison#StudyNotes#SkillVeris#ExamPrep