Awk array operations are also often used by us.
Awk can use associative array, the index can be a number or a string, awk associative arrays do not need to declare its size in advance, because it can automatically increase or decrease at runtime.
Today we will talk about the array in awk.
Array syntax
array_name[index]=value
- array_name: The name of the array
- index: The array index
- value: The value assigned by the elements in the array
Create array
In the following example, we use awk to define an array named “array”, the index of the array is a number, the value is a string. then, use the index to print the array element value.
➜ ~ awk 'BEGIN {
array[0] = "awk";
array[1] = "array";
array[2] = "linux";
print array[0] ", " array[1];
}'
awk, array
Delete array elements
In the following example, we will use the delete statement to delete the array elements, such as the delete statement below to delete index 1 and its corresponding value from the array.
➜ ~ awk 'BEGIN {
array[0] = "awk";
array[1] = "array";
array[2] = "linux";
delete array[1];
print array[1];
}'
![awk array](https://www.linuxcommands.site/wp-content/uploads/2020/05/image-41-1024x255.png)
Array length
In the following example, we will use the awk length function to print the length of the array.
➜ ~ awk 'BEGIN {
array[0] = "awk";
array[1] = "array";
array[2] = "linux";
print length(array);
}'
3
Of course, we can also use awk for loop statement, but this method is more cumbersome and not recommended.
➜ ~ awk 'BEGIN {
array[0] = "awk";
array[1] = "array";
array[2] = "linux";
array[4] = "unix";
i = 0;
for(e in array){
++i;
}
print i;
}'
4
Array add element
We have already mentioned above that the awk array can be increased or decreased automatically at runtime. Therefore, when adding an element, as long as the index is not repeated, an element will be added to the array. If the index is duplicated, it is equivalent to modifying the value corresponding to the original index.
Add array element with index 9
➜ ~ awk 'BEGIN {
array[0] = "awk";
array[1] = "array";
array[2] = "linux";
print "array length:" length(array);
array[9] = "unix";
print "add array element";
print "new array length:" length(array);
}'
array length:3
add array element
new array length:4
Modify the value of array element with index 1
➜ ~ awk 'BEGIN {
array[0] = "awk";
array[1] = "array";
array[2] = "linux";
print "value of array index 1: " array[1];
array[1] = "unix";
print "modify the value of array index 1: " array[1];
}'
value of array index 1: array
modify the value of array index 1: unix
Array sort
In the following example, we will use the awk custom function to implement bubble sort and sort the array.
➜ ~ awk '
function sort(arr) {
n = length(arr);
for(i=0;i<n;i++) {
for(j=0;j<n-1-i;j++) {
if(arr[j] < arr[j+1]) {
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
BEGIN {
array[0] = 10;
array[1] = 456;
array[2] = 1;
array[3] = 4;
sort(array);
for(i=0;i<length(array);i++) {
print array[i];
}
}
'
456
10
4
1
If you are using gawk, you can use the asorti built-in function to sort the array.
Multidimensional arrays
Awk itself does not support multi-dimensional arrays, but we can easily implement multi-dimensional arrays using one-dimensional array simulation.
For example, we simulate a 3×3 three-dimensional array:
100 200 300
200 300 400
300 400 500
In the above example, array [0] [0]=100, array [0] [1]=200, and so on. To store 100 at array [0] [0], we can use the following syntax: array [“0,0”] = 100.
➜ ~ awk 'BEGIN {
a["0,0"] = 100;
a["0,1"] = 200;
a["0,2"] = 300;
a["1,0"] = 200;
a["1,1"] = 300;
a["1,2"] = 400;
a["2,0"] = 300;
a["2,1"] = 400;
a["2,2"] = 500;
print a["2,2"];
}'
500