Awk: different between NR and FNR

As I know:

FNR: The ordinal number of the current record in the current file.

NR: The ordinal number of the current record from the start of input.

I don't understand really differency between NR and FNR. Who can explain it for me? And give me an example.

Thanks

f1:

a
b
c
d

f2:

e
f
g
awk '{printf("file->[%s] NR->[%d] FNR->[%d] str->[%s]\n", FILENAME, NR, FNR, $0)}' f1 f2

output:

file->[f1] NR->[1] FNR->[1] str->[a]
file->[f1] NR->[2] FNR->[2] str->
file->[f1] NR->[3] FNR->[3] str->[c]
file->[f1] NR->[4] FNR->[4] str->[d]
file->[f2] NR->[5] FNR->[1] str->[e]
file->[f2] NR->[6] FNR->[2] str->[f]
file->[f2] NR->[7] FNR->[3] str->[g]

5 Likes