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