Conditionals and Loops
Everything so far has relied on AWK's implicit per-line loop and pattern matching. This chapter covers the control flow constructs available inside an action block — letting an action make decisions and repeat work on its own, independent of the line-by-line structure covering the rest of this course.
if / else
Behaves exactly as in most C-family languages — braces are optional for a single statement, but using them consistently (as above) avoids a common class of mistake when adding a second statement to a branch later and forgetting to add braces at the same time.
The Ternary Operator — A Compact if/else for Expressions
Useful for short, single-value decisions inline — generally not a good substitute for a full if/else once more than one branch of logic is genuinely needed.
for Loops
AWK's C-style for loop is the standard choice for counting a fixed number of times — and combines naturally with field access, since fields are just numbered.
$ awk '{ for (i = 1; i <= NF; i++) print $i }' file.txt
$ # Sum all fields on each line
$ awk '{ sum = 0; for (i = 1; i <= NF; i++) sum += $i; print sum }' numbers.txt
for...in for arrays — a different for loop entirely
Chapter 6 already covered for (key in array) — note this is a completely different loop form from the C-style for loop above, despite sharing the keyword. The C-style form counts; the in form iterates over array keys, in unspecified order.
while Loops
$ awk '{ i = 1 while (i <= NF) { print $i i++ } }' file.txt
AWK also supports do...while, which runs the loop body at least once before checking the condition — less commonly needed than while, but available for the rare case where "run once, then keep going while true" is genuinely the right shape.
break and continue
$ awk '{ if (NF == 0) next; print }' file.txt
$ # break out of a for loop once a specific field is found
$ awk '{ for (i = 1; i <= NF; i++) { if ($i == "STOP") break print $i } }' file.txt
next isn't a loop control statement at all — it operates on AWK's implicit outer loop over input lines (Chapter 1's per-line execution model), completely separate from any for/while loop written inside an action. Using next inside a for loop skips the rest of the entire line's processing, not just the rest of that loop.
Command Reference
| Construct | Use |
|---|---|
| if / else if / else | Conditional branching |
| cond ? a : b | Compact inline conditional expression |
| for (i=1; i<=N; i++) | C-style counting loop — pairs naturally with NF |
| for (key in array) | Iterate array keys (Chapter 6) — different from the loop above despite shared keyword |
| while (cond) | Loop while a condition holds |
| break / continue | Exit or skip within the current for/while loop |
| next | Skip to the next INPUT LINE entirely — not a loop construct |
Chapter 7 Quick Reference
- if/else if/else — standard branching; use braces consistently to avoid future mistakes
- cond ? a : b — ternary, good for short single-value decisions only
- for (i=1; i<=NF; i++) — the standard idiom for "do this to every field"
- for (key in array) is a different construct entirely from the C-style for loop, despite the shared keyword
- while / do...while — condition-driven repetition
- break/continue — control the current for/while loop only
- next — skips the rest of the current LINE, not the current loop — operates on AWK's outer per-line execution
- Next chapter (final): real-world one-liners and scripts, combining AWK with grep and sed in pipelines