Chapter 8. Arrays in awk
An array is a table of values called elements. The elements of an array are distinguished by their indices. Indices may be either numbers or strings.
This chapter describes how arrays work in awk, how to use array elements, how to scan
through every element in an array, and how to remove array elements. It also
describes how awk simulates
multidimensional arrays, as well as some of the less obvious points about
array usage. The chapter moves on to discuss gawk’s facility for sorting arrays, and ends with
a brief description of gawk’s ability to
support true arrays of arrays.
The Basics of Arrays
This section presents the basics: working with elements in arrays one at a time, and traversing all of the elements in an array.
Introduction to Arrays
Doing linear scans over an associative array is like trying to club someone to death with a loaded Uzi.
—Larry Wall
The awk language provides
one-dimensional arrays for storing groups of related strings or numbers.
Every awk array must
have a name. Array names have the same syntax as variable names; any
valid variable name would also be a valid array name. But one name
cannot be used in both ways (as an array and as a variable) in the same
awk program.
Arrays in awk superficially
resemble arrays in other programming languages, but there are
fundamental differences. In awk, it isn’t necessary to specify the size of an array before starting to use it. Additionally, any number or string, not just consecutive integers, may be ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access