Take the 2-minute tour ×

I want to count all the records in a table through awk but NR prints record nos of all records not the total count. I even tried:

NF==4,count++{print count}

But its not working properly How can I do it through awk?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

Please show a sample of your file and what you want to do with it (show the desired output ) next time. Just guessing what you want,

awk 'NF==4{count++} END {print count}' file

the total number of records is indicated by NR.

awk 'END{print NR}' file1 file2

the total number of records currently is denoted by FNR.

awk 'END{print FNR}' file
share|improve this answer
+1 Your last command prints the number of records in the last file of a group of files (or only file for one file). I would use NR so the results aren't potentially confusing. If you want per-file counts: awk 'FILENAME != prevfn {print prevfn, prevc} {prevfn = FILENAME; prevc = FNR} END {print prevfn, prevc}' file* –  Dennis Williamson Feb 25 '11 at 11:38

I guess you mean total number of lines. In general, you should use wc -l <filename> for this. If you want to do this using awk, use

awk '{print NR}' | tail -1

If you only want to use awk, do

awk 'BEGIN{i=0}{i++;}END{print i}' <filename>
share|improve this answer

Your Answer


