The subscript of the awk associative array

Source: Internet
Author: User

make use variables as an array index see examples

Example

$CatEmployees
Tom Jones44245/ A/ the543354
Mary Adams5346 One/4/ the28765
Sally Chang16547/ A/ Wu650000
Billy Black.16839/ at/ -336500$ awk' {name[x++]=$2}end{for (i=0; i<nr; i++) {print i,name[i]}} 'Employees
0Jones
1Adams
2Chang
3Black

Description: the subscript for the array name is a user-defined variable x. The operator + + indicates that this is a numeric variable. awk initializes x to 0, and adds 1 after each use of X (using the post-increment operator). The 2nd field of each record is assigned to the corresponding element in the array name. The end block uses a for loop to loop through the array, starting with subscript 0, and sequentially printing the values of the array elements. The subscript is just a key, so you don't have to start with 0. The subscript can begin with any value, either a number or a string.
Example:

$ awk' {id[nr]=$3}end{for (x=1; x<=nr; x + +) {print id[x]}} 'Employees
4424
5346
1654
1683

Description:the awk variable nr holds the record number of the current record. This example uses NR as subscript to assign the 3rd field of each record to the corresponding element in the array. Finally, the For loop loops through the array, printing out the values that are stored in the arrays.

Reference: http://www.linuxawk.com/jiaocheng/252.html

The subscript of the awk associative array

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.