Iteration in AWK
AWK already loops implicitly: it reads each input record and applies every rule to it, so you rarely need an outer loop over lines. Explicit loops are for iterating *within* a record — over fields, over the characters of a string, or over the keys of an associative array. AWK provides the C-style while, do-while, and for loops, plus a special for (key in array) form for walking associative arrays. Choosing the right loop keeps your one-liners readable and avoids reinventing the record loop AWK gives you for free.
Cricket analogy: AWK's implicit record loop is like an over bowling itself ball by ball automatically; an explicit loop is you deciding to review each of the six deliveries within that over to count the dot balls.
while, do-while, and for
The while (condition) { ... } loop tests before each iteration and is the natural choice when the number of repetitions is unknown. do { ... } while (condition) runs the body at least once because the test comes after. The counting for (init; condition; update) loop is most common — for example for (i = 1; i <= NF; i++) iterates over every field of the current record, since NF holds the field count. These three share the same semantics as C, including the freedom to omit any part of the for header.
Cricket analogy: A for (i=1; i<=NF; i++) over fields is like a fielder counting exactly the eleven positions on the field — a known count, one pass per slot, just like iterating from 1 to NF.
The for-in Loop over Arrays
AWK's associative arrays are traversed with for (key in array) { ... }, which binds key to each subscript in turn. This is the standard way to print accumulated counts after a BEGIN/END aggregation. A crucial caveat is that the iteration order is unspecified — AWK does not guarantee keys come out in insertion or sorted order. If you need sorted output you must either pipe the result through sort, or in GNU AWK set PROCINFO["sorted_in"] to a comparison mode such as "@ind_str_asc".
Cricket analogy: A for (key in array) is like reading out a scorecard of run-totals per batter; but the order is unspecified, like names pulled from a hat rather than in batting order.
# sum every numeric field in each record, then tally word frequency across the file
awk '{
total = 0
for (i = 1; i <= NF; i++) total += $i # for loop over fields
print NR, "row-sum:", total
for (i = 1; i <= NF; i++) count[$i]++ # accumulate into an array
}
END {
# for-in loop over the associative array
for (word in count)
printf "%-15s %d\n", word, count[word]
}' data.txt
# break and next in action: stop at first blank field, skip comment lines
awk '/^#/ { next } # skip comments entirely
{
for (i = 1; i <= NF; i++) {
if ($i == "") break # leave the inner loop
print $i
}
}' file.txtnext and nextfile are record-level flow controls, not loop controls. next immediately stops processing the current record and reads the next one (skipping remaining rules), while nextfile skips to the next input file. Inside explicit loops, use break to exit the loop and continue to jump to the next iteration.
Never rely on the order of for (key in array). The output order is implementation-defined and can vary between AWK versions and even between runs. If your script's correctness depends on ordering, sort explicitly — via an external sort pipe or GNU AWK's PROCINFO["sorted_in"].
- AWK loops over records implicitly; explicit loops iterate within a record, string, or array.
whiletests before the body;do-whileruns the body at least once before testing.- The counting
for (i=1; i<=NF; i++)is the idiom for walking every field of a record. for (key in array)traverses associative arrays but in unspecified order.breakexits the innermost loop;continueskips to that loop's next iteration.nextskips the rest of the rules for the current record;nextfileskips the whole file.- For ordered array output, pipe through
sortor set GNU AWK'sPROCINFO["sorted_in"].
Practice what you learned
1. Which loop guarantees its body runs at least once?
2. What does `for (i = 1; i <= NF; i++)` iterate over?
3. What is true about the order of `for (key in array)`?
4. What does the `next` statement do?
5. In GNU AWK, how can you make `for (key in array)` iterate in sorted order?
Was this page helpful?
You May Also Like
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.
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.
User-Defined Functions in AWK
Learn to define reusable functions in AWK, understand its unusual parameter-passing rules — scalars by value, arrays by reference — and use the extra-parameter trick for local variables.
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