The subscript of the awk associated array, and the subscript of the awk associated array
For more information about using variables as array indexes, see examples.
Example
$ Cat employees
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500 $ awk '{name [x ++] = $2} END {for (I = 0; I <NR; I ++) {print I, name [I]} 'ployees
0 Jones
1 Adams
2 Chang
3 Black
Note: The subscript of the array name is the User-Defined variable x. Operator ++ indicates that this is a numeric variable. Awk initializes x to 0, and adds x to 1 each time (the post-incrementing operator is used ). The 2nd fields of each record are assigned to the corresponding element in the array name. The END block uses the for loop to process arrays cyclically. The values of the array elements are printed starting from the subscript 0. Subscript is only a key, so it does not have to start from 0. The subscript can start with any value, and can be a number or string.
Example:
$ Awk '{id [NR] = $3} END {for (x = 1; x <= NR; x ++) {print id [x]}' employees
4424
5346
1654
1683
Note: The awk variable NR stores the record number of the current record. In this example, NR is used as the subscript to assign the 3rd fields of each record to the corresponding element in the array. Finally, the for loop processes the array cyclically and prints the values stored in the array.
References: http://www.linuxawk.com/jiaocheng/252.html