Wednesday, November 19, 2008

Awk FNR variable usage example


I already discussed about awk FNR variable in many of my previous posts. Here is one more example for the use of NR==FNR in awk.

Description:

NR is the Number of the current Record (line) being processed.
FNR is the Number of the current Record within the current file.

So, for the first file processed they(NR, FNR) will be equal but on the first line of the second and subsequent files FNR will start from 1 again.

when NR==FNR (i.e. when processing the first file) an associative array is built up which stores the first field in an array element which also has the first field as its index.
and
when NR!=FNR (i.e. when processing the second and subsequent files) the associative array is checked to see if it has an element indexed by the first field and if so the default behavior (of printing out all of the line being processed) is carried out.


Input file:

$ cat file.txt
10 AD
20 NA
30 PS
50 KR

Required:

Add a third column to the above file which is the sum of first field elements.
i.e. required output:

10 AD 110
20 NA 110
30 PS 110
50 KR 110

awk solution:

$ awk 'NR==FNR{sum+=$1;next}$0=$0 FS sum' file.txt file.txt

10 AD 110
20 NA 110
30 PS 110
50 KR 110

Related posts for NR==FNR in awk:

- Match words between two file using awk
- Perform join using awk
- Update a file based on another file using sed
- Delete lines based on another file using awk
- Update file based on another file using awk

No comments:

© Jadu Saikia www.UNIXCL.com