Difference between awk NR and FNR variable

From awk(1) man pages, the definition of awk NR and FNR variables are:

NR ordinal number of the current record
FNR ordinal number of the current record in the current file

For a single file, FNR is nothing but NR (line number)

e.g.

$ cat main.txt
ID1:2.45
ID2:21.5
ID4:1.32
ID3:89.2
ID7:12.4
ID9:13
ID5:19.8

$ awk 'BEGIN {print "NR\tFNR\tline"}
{print NR"\t"FNR"\t"$0}
' FS=: main.txt

NR FNR line
1 1 ID1:2.45
2 2 ID2:21.5
3 3 ID4:1.32
4 4 ID3:89.2
5 5 ID7:12.4
6 6 ID9:13
7 7 ID5:19.8


And for more than one file, NR and FNR will be equal for the first processed file, but on the first line of the second and subsequent files FNR will start from 1 again.

Lets have one more file named filter.txt

$ cat subfile.txt
ID9
ID3
ID4

Now, lets print the same NR, FNR and the lines with both the files combined.

$ awk 'BEGIN {print "NR\tFNR\tline"}
{print NR"\t"FNR"\t"$0}
' FS=: subfile.txt main.txt

NR FNR line
1 1 ID9
2 2 ID3
3 3 ID4
4 1 ID1:2.45
5 2 ID2:21.5
6 3 ID4:1.32
7 4 ID3:89.2
8 5 ID7:12.4
9 6 ID9:13
10 7 ID5:19.8


You might be wondering, what can be the use of awk NR and FNR combination. I already did a few posts using NR==FNR functionality.

Lets discuss one of them.

$ cat main.txt
ID1:2.45
ID2:21.5
ID4:1.32
ID3:89.2
ID7:12.4
ID9:13
ID5:19.8

$ cat subfile.txt
ID9
ID3
ID4

Requirement: We need to delete those rows from main.txt for which the id field (1st field) has an entry in subfile.txt.

so, required output:

ID1:2.45
ID2:21.5
ID7:12.4
ID5:19.8

The solution is:

$ awk 'NR==FNR{A[$1];next}!($1 in A)' FS=: subfile.txt main.txt

So, associative array named A is created with the Ids of 1st processed file (subfile.txt, for which NR==FNR, as it is first in the sequence "subfile.txt main.txt" above).

To confirm,

$ awk 'NR==FNR {print $0,FILENAME}' FS=: subfile.txt main.txt

ID9 subfile.txt
ID3 subfile.txt
ID4 subfile.txt

So associative array A will contain the elements ID9 ID3 and ID4.

We proceed to the next lines and printing only those of remaining rows, which does not (!) have 1st filed as any one of the elements of the associative array A.


[FROM]http://unstableme.blogspot.com/2009/01/difference-between-awk-nr-and-fnr.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值