Friday, January 16, 2009

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.

Related posts:

- awk FNR variable usage example
- update file based on another file in awk
- match words, bash newbie

1 comment:

Karan Bohra said...

echo '.' | { cat id.txt - main.txt; } |
sed -ne '
1{
:id
H;1h;n
/^[.]$/d
bid
}
# main
G
/^\([^:]*\):.*\n\1\n/d
/^\([^:]*\):.*\n\1$/d
P
'

© Jadu Saikia www.UNIXCL.com