Reader small image

You're reading from  Learning AWK Programming

Product typeBook
Published inMar 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788391030
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Shiwang Kalkhanda
Shiwang Kalkhanda
author image
Shiwang Kalkhanda

Shiwang Kalkhanda (RHCA, RHCSS, MCSE) is a Linux geek and consultant with expertise in the automation of infrastructure deployment and management. He has more than 10 years' experience in security, system, and network administration, and training on open source tech. For most of his automation work, he uses Shell Scripting, Python, and Go. He holds a master's and a bachelor's degree in computer applications. He enjoys traveling and spending time with his children. He is also the author of a book on text processing utilities in Unix-like environments, Learning Awk Programming.
Read more about Shiwang Kalkhanda

Right arrow

Working with Arrays in AWK

An array is a variable that is used to store a set of values (strings or numbers). These values, or independent elements, are accessed by their index in the array. Indexes are stored in square brackets and may be either numbers or strings. This chapter focuses on how arrays are implemented in AWK.

In this chapter, we will cover the following topics:

  • One-dimensional arrays in AWK
  • Assigning and accessing elements in arrays
  • Referring to array elements
  • Processing arrays using loops
  • Creating an array using a split function
  • Delete operations in arrays
  • Multidimensional array implementation in AWK

One-dimensional arrays

The AWK language provides one-dimensional arrays for storing strings and numbers. An array name could be any valid variable name. One variable name cannot be used as both an array and a variable at the same time in the same program.

Arrays in AWK are extremely powerful in comparison to traditional arrays that we use in other programming languages. Arrays in AWK are associative—that is, each array is a collection of a pair: an index and its corresponding array element value. In associative arrays, indexes are not essentially required to be in order, one can use either a string or a number as an array index. An array size can expand or shrink at runtime and is not statically defined.

Its syntax is as follows:

arr[index] = value

The different elements of the array syntax used here are explained in the following list:

  • arr: This is the name of the array...

Assignment in arrays

Array elements can be assigned values like any other AWK variables, as follows:

arr[index] = value

In the following example, we take the cars database file cars.dat. Here, we make the record number the index to the array and store each record as a value to the corresponding array element. The system variable NR is used as the index for the array as it gets incremented for each record. In the end, we print the array elements using a for loop as follows:

$ vi basic_array.awk

{
arr[NR] = $0
}
END{
for ( x=1; x<= NR; x++)
print "index : "x, "value :"arr[x]
}

$ awk -f basic_array.awk cars.dat

The output of the execution of the preceding code is as follows:

index : 1 value :maruti          swift       2007        50000       5
index : 2 value :honda city 2005 60000 3
index : 3 value :maruti dezire...

Accessing elements in arrays

The ideal way to access an array element is to refer to that element via its index, as follows:

arr[index]

Here, arr is the name of an array, and index is the index of the desired element of the array that we want to access.

The following is a simple example of assigning and accessing AWK arrays:

$ vi arr_access.awk

BEGIN {
arr[30] = "volvo"
arr[10] = "bmw"
arr[20] = "audi"
arr[50] = "toyota"
arr["car"] = "ferrari"
arr[70] = "renault"
arr[40] = "ford"
arr[80] = "porsche"
arr[60] = "jeep"
print "arr[10] : ", arr["10"]
print "arr[car] : ", arr["car"]
print "arr[80] : ", arr["80"]
print "arr[30] :...

Referring to members in arrays

We can directly display the value stored in an array element using the print command, or we can assign it to another variable for further processing inside a AWK program as follows:

$ vi arr_var_assign.awk

BEGIN {
arr[10] = "maruti"
arr[20] = "audi"
print "arr[10] : " arr[10]
x=arr[20] print "x : " x
}

$ awk -f arr_var_assign.awk

The output of the execution of the preceding code is as follows:

arr[10] : maruti
x : audi

To check whether a particular index exists in an array, we use the if condition within the operator to build the conditional expression syntax, as shown in the following syntactical phrase. It will return true (1), if the index exists in the array; otherwise, it will return false (0):

if(index in array)

In the following example, we show you how the if condition works when...

Processing arrays using loops

If we have to access all the array elements, we can use a loop that executes once for each element of the array. In other programming languages, where indexes are sequentially numbered, a simple for loop construct is used to access the elements of array. Here, AWK has an associative array, so we use a special type of for loop to go through all the indexes of an array. Its syntax is as follows, followed by a listed explanation of the elements involved:

for (var in array)

body of loop

  • var: This is any variable name, which is set to the index of the corresponding array element.
  • in: This is a keyword.
  • array: This is an array name.
  • body of loop: This is a list of AWK statements that are to be executed. If you want to execute more than one action, it needs to be enclosed within braces. The loop will execute until there is an index element in the array...

Using the split() function to create arrays

The built-in split() function can parse any string into elements of an array. The split(string, arr, fs) function splits the string value of str into fields and stores them in the arr array. The number of fields produced is returned as the value of the split function. The string value of the third argument, fs, determines the field separator. The syntax of split() functions is as follows, followed by a listed explanation of the elements involved:

n = split (str, arr, fs)

  • str: This is the input string to be parsed into the elements of the named arr.
  • arr: This is the name of the array.
  • fs: This is the separator character based on which the array elements are split. If the separator is not given, the elements are split based on the fs as the separator. The separator can be a single character of the regular expression.
  • n: This is the index...

Delete operation in arrays

The delete command is used to remove the individual element from an array. Once an element from an AWK array is deleted, we cannot obtain its value any longer. The syntax of the delete statement is as follows:

delete arr[index];

In the following example, we delete the array element with the car index and print all the remaining elements using the for loop, as follows:

$ vi arr_delete.awk

BEGIN {
arr[10] = "maruti"
arr[20] = "audi"
arr["car"] = "ford"
arr[30] = "ferrari"
arr[40] = "porsche"

delete arr["car"]
for ( v in arr )
print v,arr[v]
}

$ awk -f arr_delete.awk

The output of the execution of the preceding code is as follows:

10 maruti
20 audi
30 ferrari
40 porsche

The following for loop command removes all elements from an arr array:

for (v in arr)
...

Multidimensional arrays

AWK supports one-dimensional arrays only. However, we can simulate multidimensional arrays using one-dimensional arrays. Let us create a multidimensional array as follows:

$ vi multi_arr1.awk

BEGIN{
arr["1,1"] = 10
arr["1,2"] = 20
arr["2,1"] = 30
arr["2,2"] = 40
arr["3,1"] = 50
arr["3,2"] = 60

for ( v in arr ) print "Index ",v, " contains "arr[v]
}

<strong>$ awk -f multi_arr1.awk

The output of the execution of the preceding code is as follows:

Index  1,1  contains 10
Index 1,2 contains 20
Index 2,1 contains 30
Index 2,2 contains 40
Index 3,1 contains 50
Index 3,2 contains 60

In the preceding example, we have given the arr["1,1"] array as the index. It is not two indexes, as would be the case in a true multidimensional...

Summary

In this chapter, we learned about arrays. We learned that AWK provides one-dimensional associative arrays (arrays indexed by string values). We also learned how array elements are referenced using arr[index], and that it creates the element if it does not exist. Then we used the for loop to scan through all the individual elements of an array, and tested the array membership using the in operator with the if expression. We used the split function to create an array. We also learned how to delete an individual element or a whole array using the delete command. Finally, we covered how AWK simulates multidimensional arrays by separating subscript values with commas. In this index, values are concatenated into a single string, separated by the value of SUBSEP.

In the next chapter, we will learn about how to do pretty printing in AWK using formatted reports, and we will learn...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning AWK Programming
Published in: Mar 2018Publisher: PacktISBN-13: 9781788391030
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Shiwang Kalkhanda

Shiwang Kalkhanda (RHCA, RHCSS, MCSE) is a Linux geek and consultant with expertise in the automation of infrastructure deployment and management. He has more than 10 years' experience in security, system, and network administration, and training on open source tech. For most of his automation work, he uses Shell Scripting, Python, and Go. He holds a master's and a bachelor's degree in computer applications. He enjoys traveling and spending time with his children. He is also the author of a book on text processing utilities in Unix-like environments, Learning Awk Programming.
Read more about Shiwang Kalkhanda