Basic usage of the awk array in Linux

  • 2020-11-25 07:49:32
  • OfStack

1.awk array description

In other programming languages, the array subscript is 0, that is to say, if you want to back references the first element in the array, you need to refer to the corresponding index [0], in awk array is also, by using the method of reference subscript but in the array subscript awk began from 1, in other languages, you might be used to "declaration" first one array, in awk, shouldn't have done this, directly for the elements of an array of assignment can be (in fact, if oneself give array assignment, the subscript 1 or starting from 0 it doesn't matter!)

2. When declaring an array, the value may be too much, and the command is too long, which reduces the readability of the command. Therefore, the backslash "\" is used to break the line, and the effect is exactly 1, as shown in the code below:


[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three"\
;arr[4]="four";print arr[3]}'
three
[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three";arr[4]="four";print arr[3]}'
three

3. The element of array is set to null, which is allowed. When there is no element in the array but directly references it, it will be assigned to null by default, so the method of determining whether an element exists should not be used.


[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three";if(4 in arr){print "four in this arr"}}'
[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three";if(3 in arr){print "three in this arr"}}'
three in this arr

You can also take the inverse (using the operator!).


[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three";if(!(4 in arr)){print "four not in this arr"}}'
four not in this arr

4.awk array index

In awk array subscript is not only can be a "number", can also be "arbitrary string", in fact, awk is an associative array, the array is first to use Numbers as a subscript for example is in order to facilitate the habit of before, can have a good transition, however, with Numbers as an array subscript in some scenarios have 1 set of advantages, but in essence is "an associative array", awk will default to convert the "number" subscript "string", so it is essentially a string as the subscript "associative array"

5. Delete array elements

You can remove elements from an array using delete, or you can remove the entire array using delete


[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three";\
> delete arr[1];print arr[1]}'
____ (empty) 
[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three";\
print arr[1];print arr[3];delete arr;print arr[1]}'
one
three
____ (empty) 

6. Loop through the groups using for

Syntax: for (variable in array name) {code statement}

Note: the variable loop is the index of the array


[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three";arr[4]="four";\
> for ( i in arr){print arr[i]}}'
four
one
two
three
# Unordered print array elements, enter 1 Step by step it's an associative array. 
# Ordered print array elements 
[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three";arr[4]="four";\
for ( i=1;i<=4;i++){print arr[i]}}'
one
two
three
four

7.awk array use instance


[zkpk@master as]$ awk 'BEGIN{a=1;print a; a=a+1 ; print a}'
1
2
[zkpk@master as]$ awk 'BEGIN{a=1;print a; a++ ; print a}'
1
2

If you set the variable a to 1 and add it to itself, the value will increase by 1, which is not hard to understand. So what if the variable a is a string?


[zkpk@master as]$ awk 'BEGIN{a="test";print a; a++ ; print a}'
test

When the value of a is a string, it can also be evaluated, and it can be seen that the string is evaluated as a number 0, so the empty string is evaluated as a zero.


[zkpk@master as]$ awk 'BEGIN{a="";print a; a++ ; print a}'
____ (empty) 

, according to the results of an empty string when participating in operation will be as 0, we have said before, when we refer to a nonexistent element in an array, the element is assigned to an empty string, when to since this element to add operation, the value of the element becomes 1, so that when we are on a nonexistent element after add operation, the value of this element becomes the plus the number of times since x times, the value of the element is assigned for x, since the add y value of the time element is assigned for y, So we can use this feature of the awk array to count the number of occurrences of a string in the text, as shown below


[zkpk@master as]$ cat text
Alice
Bob
Tom
Peter
Alice
Alice
Tom
Bob
Peter
Bob
[zkpk@master as]$ awk '{count[$1]++};END{for(i in count){print i,count[i]}}' text
Bob 3
Tom 2
Alice 3
Peter 2

It's time to see the power of the awk array. Well, you might say That I can count without awk, as shown in the code below:


[zkpk@master as]$ cat text | sort | uniq -c
   3 Alice
   3 Bob
   2 Peter
   2 Tom

Okay, I agree that's a great idea, but what about the following example? Count the number of times a person's name appears in the text


[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three";if(4 in arr){print "four in this arr"}}'
[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three";if(3 in arr){print "three in this arr"}}'
three in this arr
0

But if you do not use awk, you must use another command implementation. You can refer to the following code (^_^)


[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three";if(4 in arr){print "four in this arr"}}'
[zkpk@master as]$ awk 'BEGIN{arr[1]="one";arr[2]="two";arr[3]="three";if(3 in arr){print "three in this arr"}}'
three in this arr
1

At the end

awk array were introduced in this paper the basic method of use, but to learn the use of flexible, I also write in the above example 1 some can replace awk array way to some extent, so this paper is not just awk array, how to use the right scenario, but how to choose the optimal solution, fast and efficient to solve the problem. This is what I have always pursued, and this is my real intention to learn the Linux command.


Related articles: