A summary of the differences between NR and Fnr in Awk and an example demo _linux shell

Source: Internet
Author: User

One, the difference between NR and FNR

NR: Represents the current number of records
FNR: Also represents the current number of records, but the scope of FNR is within only one file. If you reopen the file, FNR starts at 1.

Second, the example illustrates

1, test file AAA,CCCC

Copy Code code as follows:

[Root@blackghost test2]# Cat AAA//test File AAA
1111:23434:zhang
Hoadsf:asdf:ccc
[Root@blackghost test2]# Cat CCC//test document CCC
1111:23434:zhang
Hoadsf:asdf:ccc
tank:zhang:x20342
ying:zhasdf:72342
Hosa:asdfa:2345sdf

2, a file in Nr,fnr

Copy Code code as follows:

[Root@blackghost test2]# awk ' {print nr;print Fnr;print $} ' AAA
1//NR
1//FNR
1111:23434:zhang
2
2
Hoadsf:asdf:ccc

In the example above, we can see that NR and Fnr are the same

3, NR,FNR in multiple files

Copy Code code as follows:

[Root@blackghost test2]# awk ' {print nr;print Fnr;print $} ' AAA CCC
1
1
1111:23434:zhang
2//NR
2//FNR
Hoadsf:asdf:ccc
3//NR
1//fnr The data below is from CCC, so NFR reset to 1
1111:23434:zhang
4
2
Hoadsf:asdf:ccc
5
3
tank:zhang:x20342
6
4
ying:zhasdf:72342
7
5
Hosa:asdfa:2345sdf

See how different it is, when multiple file operations, when a new open file Fnr will start again from 1

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.