Arrays

An array is a variable that can be used to store a set of values. Usually the values are related in some way. Individual elements are accessed by their index in the array. Each index is enclosed in square brackets. The following statement assigns a value to an element of an array:

array[subscript] = value

In awk, you don’t have to declare the size of the array; you only have to use the identifier as an array. This is best done by assigning a value to an array element. For instance, the following example assigns the string "cherry" to an element of the array named flavor.

flavor[1] = "cherry"

The index or subscript of this element of the array is “1”. The following statement prints the string “cherry”:

print flavor[1]

Loops can be used to load and extract elements from arrays. For instance, if the array flavor has five elements, you can write a loop to print each element:

flavor_count = 5
for (x = 1; x <= flavor_count; ++x)
	print flavor[x]

One way that arrays are used in awk is to store a value from each record, using the record number as the index to the array. Let’s suppose we wanted to keep track of the averages calculated for each student and come up with a class average. Each time a record is read we make the following assignment.

student_avg[NR] = avg

The system variable NR is used as the subscript for the array because it is incremented for each record. When the first record is read, the value of avg is placed in student_avg[1]; for the second record, the value is placed ...

Get sed & awk, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.