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.
# 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.csvThe 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 arrayto 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
1. What kind of arrays does AWK provide?
2. Why prefer `if ("x" in arr)` over `if (arr["x"])` to test existence?
3. What is `grid[i, j]` shorthand for in standard AWK?
4. What does `length(arr)` return when arr is an array?
5. Which statement removes a single element from an AWK array?
Was this page helpful?
You May Also Like
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.
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.
String Functions in AWK
Explore AWK's built-in string functions — length, substr, index, split, sub, gsub, match, sprintf, and case conversion — for extracting, searching, and transforming text.
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