Conditionals and Loops

Chapter 7
Control Flow
if/else, for loops, and while loops — the constructs that make AWK a genuine programming language, not just a pattern matcher

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

$ awk '{ if ($3 > 100) { print $1, "high value" } else if ($3 > 50) { print $1, "medium value" } else { print $1, "low value" } }' sales.csv

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

$ awk '{ status = ($3 > 100) ? "high" : "low"; print $1, status }' sales.csv

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.

$ # Print every field on its own line, regardless of how many fields exist
$ 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 (i = 1; i <= NF; i++) is the standard idiom for "do something to every field"
Because NF (Chapter 2) gives the exact field count for the current line, this loop pattern correctly handles lines with any number of fields without hardcoding a number — genuinely one of the most reused patterns in everyday AWK scripts.

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

$ # Equivalent to the for-loop field example above, written with while
$ 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

break
Exits the innermost loop immediately, skipping any remaining iterations entirely.
continue
Skips the rest of the current iteration only, then continues with the next one — the loop keeps running.
next
Specific to AWK's outer per-line loop (not the for/while loops above) — skips the rest of the current LINE's processing entirely, moving straight to the next line of input.
$ # Skip blank lines entirely — next applies to the outer per-line loop
$ 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 skips the rest of the CURRENT LINE's action, not the current loop iteration
A common confusion: 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

ConstructUse
if / else if / elseConditional branching
cond ? a : bCompact 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 / continueExit or skip within the current for/while loop
nextSkip 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