The subscript of the awk associated array, and the subscript of the awk associated array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.