Capture the last record number using "awk" NR variable

Hi Team. I am trying to capture the last record number from a file using the below command ( assuming abc.txt has 21 records and I want 21 as output )

awk'{c=NR;print c}'abc.txt

But it is printing all the record number. Can someone please help modify the above command?

Consider

wc -l

which is meant to give number of records.

count=`wc -l < abc.txt`

awk is a great tool. However there are lots of useful unix tools, which you may want to consider

1 Like

If it is a trivial count, then commands like wc can give you this, however I think the easiest way using awk (if you are really doing some processing of the data in awk) is to have a count incremented each time you read a record, then in the END section, display the count.

Would this work for you?:-

awk '{c=NR;print c ; records++} END {print records}' abc.txt

Obviously adjust it to your preferences. The above will print a line number for every record read AND the final record count, so this might not be what you want. I would only go with awk if you are doing some other processing. wc is far simpler.

I hope that this helps,
Robin

1 Like

Thanks Jim. Actually I want to use the last record number in a for loop within awk command.

Ex :

awk'{for(i=1;i<=NR;i++) <action> }'abc.txt

Actually am assuming that the loop is running from 1 to 21 for the above

The number of records which is NR contains the current line number. awk has an END{} block, where when it is reached NR will have the number 21 as value in your case.
Anyway, I second what the others say, that the least complex tool needed to do the job might be the tool of choice.

If I got it right, you just want to read an input file and do something with every line.

This could generally also be done with something like this:

while read LINE; do
    echo $LINE
done < infile

...where the echo could be something else of course.
This way you don't even need to know how many lines you have to use it in a for -loop, as it cycles over every line anyway.

1 Like

awk already does that, implicitly.

Pseudocode for an awk program:

Execute BEGIN statement

for(FILENAME in ALL_FILES)
{
        for($0 in ALL_RECORDS)
        {
                YOUR_CODE
        }
}

Execute END statement

So you just need something to save $0 in YOUR_CODE, and something to print it in the END statement.

1 Like

If you just want to print the number of lines in a file, and you're unwilling to use:

wc -l < file

and have to use awk , the simple way to do it would be:

awk 'END{print NR}' file

Expanding a bit on what Corona688 said, and commenting on your own proposal in post#4: See what happens if your (syntactically corrected) code (action "print" assumed) works on a file like

cat file
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 A B C D E F
awk '{for(i=1;i<=NR;i++) printf "%s ", $i; printf RS}' file
0 
0 1 
0 1 2 
0 1 2 3 
0 1 2 3 4 
0 1 2 3 4 5 
0 1 2 3 4 5 6 
0 1 2 3 4 5 6 7 

In awk one normally loops over the fields. NF is the number of fields.