Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using awk to count number of records

Tags:

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?

like image 695
Shweta Avatar asked Feb 25 '11 07:02

Shweta


People also ask

How do I count the number of fields in awk?

awk with NF (number of fields) variable. NF is a built-in variable of awk command which is used to count the total number of fields in each line of the input text. Create any text file with multiple lines and multiple words.

Which of the following is used to print number of records in awk in Linux?

NR: NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file.

Which awk command will give you the number of lines in a file?

wc. The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.


1 Answers

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 
like image 180
kurumi Avatar answered Sep 22 '22 10:09

kurumi