FNR: it also indicates the number of current records, but FNR is only in one file. if the file is re-opened, FNR starts from 1.
1. differences between NR and FNR
NR: indicates the number of current records.
FNR: it also indicates the number of current records, but the FNR scope is only in one file. if you re-open the file, FNR starts from 1.
II. instance description
1. test file aaa and cccc
[Root @ Blackghost test2] # cat aaa // test file aaa1111: 23434: zhanghoadsf: asdf: ccc [root @ Blackghost test2] # cat ccc // test file ccc1111: 23434: zhanghoadsf: asdf: ccctank: zhang: x20342ying: zhasdf: 72342 hosa: asdfa: 2345sdf
2. NR and FNR in a file
[root@Blackghost test2]# awk '{print NR;print FNR;print $0;}' aaa1 //NR1 //FNR1111:23434:zhang22hoadsf:asdf:ccc
In the above example, we can see that NR and FNR are the same
3. NR and FNR in multiple files
[Root @ Blackghost test2] # awk '{print NR; print FNR; print $0;} 'AAA ccc111111: 23434: zhang2 // NR2 // FNRhoadsf: asdf: data under ccc3 // NR1 // FNR is from ccc, so NFR is reset to 11111: 23434: zhang42hoadsf: asdf: ccc53tank: zhang: x2034policying: zhasdf: 7234275 hosa: asdfa: 2345sdf
See the difference. when multiple files are operated, FNR will start from 1 again when the file is opened.