Examples of awk built-in variables: Examples of NR, FNR, and NF, awkfnr

Source: Internet
Author: User

Examples of awk built-in variables: Examples of NR, FNR, and NF, awkfnr

NR indicates the number of data reads Based on the record delimiter after running from awk. The default record delimiter is a line break. Therefore, the default value is the number of data rows read, NR can be interpreted as the abbreviation of Number of Record.

When awk processes multiple input files, after the first file is processed, NR does not start from 1, but continues to accumulate, so FNR appears, every time a new File is processed, FNR starts counting from 1. FNR can be interpreted as File Number of Record.

NF indicates the Number of fields split by the current record. NF can be interpreted as Number of Field.

The following describes the example program. First, prepare two input files class1 and class2, and record the scores of the two classes. The content is as follows:

CodingAnts@ubuntu:~/awk$ cat class1zhaoyun 85 87guanyu 87 88liubei 90 86CodingAnts@ubuntu:~/awk$ cat class2caocao 92 87 90guojia 99 96 92

Now you can use the following awk command to view all the score information of the two classes and add the row number before each message;

CodingAnts@ubuntu:~/awk$ awk '{print NR,$0}' class1 class21 zhaoyun 85 872 guanyu 87 883 liubei 90 864 caocao 92 87 905 guojia 99 96 92

The row number here is implemented through NR. Every time an awk reads a record, the NR value is added. To change the row number of each class from the beginning, use FNR as follows:

CodingAnts@ubuntu:~/awk$ awk '{print FNR,$0}' class1 class21 zhaoyun 85 872 guanyu 87 883 liubei 90 861 caocao 92 87 902 guojia 99 96 92

The following example uses the awk built-in variable FILENAME to better differentiate the scores of the two classes;

CodingAnts@ubuntu:~/awk$ awk '{print FILENAME,"NR="NR,"FNR="FNR,"$"NF"="$NF}' class1 class2class1 NR=1 FNR=1 $3=87class1 NR=2 FNR=2 $3=88class1 NR=3 FNR=3 $3=86class2 NR=4 FNR=1 $4=90class2 NR=5 FNR=2 $4=92

In addition to NR and FNR, the above example also demonstrates the use of NF. Each row in class1 has three fields, while class2 has four fields, with $ NF, you can easily obtain the last field.

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.