NR versus FNR in awk scripts
I have got a data file that contains 22 records.
When i write an awk script to compute the number of records, i print the value of NR variable. Why does it show 23 instead of 22? I get the same output from FNR variable also. So what is the difference between NR and FNR variable? In the text i found that NR: gives number of records read- record number in the current file FNR: file number of records read- a record number in the current file |
This may be clearer on the difference:
Quote:
Also, remember that awk reads every line of the file so if any line is blank it is still a record according to awk. |
22 records meant that there was a single column with 22 rows. these rows were containing age values.
example: 23 33 44 65 54 etc. there were 22 such values one in each row If NR value starts from 0 then for 22 such values read, the final value should be 21. why am i getting 23 then? |
Well firstly, NR starts at 1 for the first line. Assuming that there are no blank lines, can you also confirm the file was not created on a windows machine?
If not created on windows, you would need to provide the data and the awk command you are using. |
All times are GMT -5. The time now is 05:48 AM. |