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

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.

Control Flow & FunctionsBeginner9 min readJul 10, 2026
Analogies

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.

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

next 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.
  • while tests before the body; do-while runs 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.
  • break exits the innermost loop; continue skips to that loop's next iteration.
  • next skips the rest of the rules for the current record; nextfile skips the whole file.
  • For ordered array output, pipe through sort or set GNU AWK's PROCINFO["sorted_in"].

Practice what you learned

Was this page helpful?

Topics covered

#Programming#AWKStudyNotes#LoopsInAWK#Loops#AWK#Iteration#While#StudyNotes#SkillVeris#ExamPrep