LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-18-2015, 10:44 PM   #1
Upendra Pratap Singh
Member
 
Registered: May 2011
Location: India
Distribution: Ubuntu 14.04
Posts: 110
Blog Entries: 4

Rep: Reputation: 0
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
 
Old 02-18-2015, 11:47 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
This may be clearer on the difference:
Quote:
The awk utility divides the input for your awk program into records and fields. awk keeps track of the number of records that have been read so far from the current input file. This value is stored in a built-in variable called FNR. It is reset to zero when a new file is started. Another built-in variable, NR, records the total number of input records read so far from all data files. It starts at zero, but is never automatically reset to zero.
As for the different number of records, you will need to explain further what you mean by '22 records'?
Also, remember that awk reads every line of the file so if any line is blank it is still a record according to awk.
 
Old 02-19-2015, 12:50 AM   #3
Upendra Pratap Singh
Member
 
Registered: May 2011
Location: India
Distribution: Ubuntu 14.04
Posts: 110
Blog Entries: 4

Original Poster
Rep: Reputation: 0
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?

Last edited by Upendra Pratap Singh; 02-19-2015 at 12:54 AM. Reason: to provide template of the data file that i am using
 
Old 02-19-2015, 07:59 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
turning awk command line entries into awk scripts tabbyagirl Linux - Newbie 5 08-22-2013 12:46 AM
awk scripts jdeeptir Linux - Newbie 9 06-09-2008 01:18 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 05:39 PM
Value counting in awk scripts sarajevo Programming 9 10-20-2006 08:24 AM
Help needed in writing Awk Scripts.. TheDarktrooper Red Hat 1 05-04-2004 06:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration