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

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.

Control Flow & FunctionsIntermediate10 min readJul 10, 2026
Analogies

Associative Arrays: AWK's Core Data Structure

Every array in AWK is associative: subscripts are strings, not just integers, and the array is essentially a hash map. You never declare or size an array — referencing count["apple"] or count[$1] creates the entry on first use, and numeric subscripts like a[5] are silently converted to the string "5". This makes arrays the workhorse for counting, grouping, and building lookups. A single line such as count[$1]++ accumulates a frequency table keyed by the first field, and the pattern is so common it defines idiomatic AWK.

🏏

Cricket analogy: An associative array is like a scorer's book keyed by batter name rather than position: runs["Kohli"] += 4 adds a boundary to that player's tally, no matter where he bats in the order.

Membership, Deletion, and length

To test whether a key exists without creating it, use the in operator: if ("apple" in count). This is important because simply writing if (count["apple"]) would create the entry with an empty/zero value as a side effect. To remove entries use delete array[key] for one key or delete array (GNU AWK) to clear the whole array. In modern AWK the length(array) function returns the number of elements, which is handy for counting distinct keys — for instance the number of unique values you have seen.

🏏

Cricket analogy: The in operator is like checking the team sheet to see if a player is in the squad without adding him — "Bumrah" in squad — whereas indexing blindly would accidentally cap an uncapped player.

Multi-Dimensional Arrays and SUBSEP

AWK simulates multi-dimensional arrays by joining subscripts into a single string key. Writing grid[i, j] is shorthand for grid[i SUBSEP j], where SUBSEP is a special separator variable defaulting to the non-printing character \034. You can test membership with if ((i, j) in grid) and iterate with for (k in grid) then split(k, parts, SUBSEP) to recover the components. GNU AWK also offers true multi-dimensional arrays of arrays (grid[i][j]), but the SUBSEP technique is the portable, POSIX-standard approach.

🏏

Cricket analogy: A grid[over, ball] key is like referring to a delivery by 'over 14, ball 3' — two coordinates fused into one label to locate a specific ball in the innings, like SUBSEP joining subscripts.

awk
# frequency table: count occurrences of each value in column 1
awk '{ count[$1]++ }
END {
    for (key in count)
        printf "%-20s %d\n", key, count[key]
}' access.log

# safe membership test vs. accidental creation
awk '{
    if ($1 in seen)
        print $1, "is a duplicate"
    else
        seen[$1] = 1
}' ids.txt

# emulated 2D array: sales per region per quarter
awk -F',' 'NR > 1 { sales[$1, $2] += $3 }
END {
    for (k in sales) {
        split(k, p, SUBSEP)
        printf "region=%s quarter=%s total=%d\n", p[1], p[2], sales[k]
    }
    print "distinct region-quarter pairs:", length(sales)
}' sales.csv

The count[$1]++ idiom is the single most useful AWK pattern. Combined with an END block that iterates the array, it replaces whole pipelines of sort | uniq -c while giving you full control over formatting, filtering, and secondary computation on the counts.

Testing a key with if (arr[k]) instead of if (k in arr) silently creates arr[k] with a null value. In loops that later iterate the array, these phantom empty entries can corrupt counts and produce spurious output. Always use the in operator for existence checks.

  • All AWK arrays are associative: string-keyed hash maps that grow on demand.
  • Referencing a subscript creates it; numeric subscripts are converted to strings.
  • count[$1]++ is the idiomatic pattern for building frequency tables.
  • Use key in array to test existence without creating the key.
  • delete array[key] removes one entry; delete array (GNU AWK) clears all.
  • Multi-dimensional arrays are emulated by joining subscripts with SUBSEP: a[i,j].
  • length(array) returns the number of elements — useful for counting distinct keys.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#AWKStudyNotes#ArraysInAWK#Arrays#AWK#Associative#Core#DataStructures#StudyNotes#SkillVeris