Getline of awk

Source: Internet
Author: User
To learn more about getline in awk getline, you must first understand the concepts of $0, NR, and FNR. The so-called NR can be understood as NumberofRecord, or a row number, while FNR can be understood as NumberofRecordinFile, or become a row number in the file. it sounds no different... getline of awk
To learn more about getline, you must first understand the concepts of $0, NR, and FNR.
The so-called NR can be interpreted as Number of Record, or a row Number, while FNR can be interpreted as Number of Record in File or a row Number in the File. it sounds no different. In fact, if you only have one file for awk processing, NR and FNR are the same thing, as shown below, the same file bbs-list-3, for each row of output NR and FNR are the same.
Manu @ manu :~ /Code/shell/awk $ cat bbs-list-3 unzip dvark 555-5553 1200/300 B alpo-net 555-3412 2400/1200/300 A barfly 555-7685 1200/300 A manu @ manu :~ /Code/shell/awk $ awk '{printf "%-2d %-2d % s \ n", NR, FNR, $0} 'bbs-list-3 1 1 aardvark 555-5553 1200/300 B 2 2 alpo-net 555-3412 2400/1200/300 A 3 3 barfly 555-7685 1200/300 A but if you have multiple files to process, the situation is different. No matter which file you are in, NR ++ reads each row. However, if the file to be processed changes, FNR starts counting again from 1, as shown in:
 
 

Valid tive awk programming provides a table to summarize the usage of awk as follows:

What does this mean? let's take a few examples to illustrate the usage of getline and explain why the output is like this: first, our reference file is:
 

1 getline
Let's take a look at the table above. getline will assign the next record to $0. at the same time, NR and FNR will all be ++.
 

2 getline var
This corresponds to the second record of the table. getline var will assign the next record to var, NR ++, and FNR ++, without any change to $0.
Look, every time you get the next record and assign it to tmp, $0 never changes, because the getline var mode never changes $0.
 

3 command | getline
As shown in the table, $0 is affected, and the result of executing command is assigned to $0. for copying, we can use the following example:
 

4 command | getline var
If this is not mentioned, the command output will be assigned to var, while $0 remains unchanged.
 
Related Article

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.