Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count number of lines in terminal output

Tags:

bash

terminal

couldn't find this on SO. I ran the following command in the terminal:

>> grep -Rl "curl" ./ 

and this displays the list of files where the keyword curl occurs. I want to count the number of files. First way I can think of, is to count the number of lines in the output that came in the terminal. How can I do that?

like image 639
roopunk Avatar asked Sep 17 '12 10:09

roopunk


People also ask

How do I count lines in terminal?

The most easiest way to count the number of lines, words, and characters in text file is to use the Linux command “wc” in terminal. The command “wc” basically means “word count” and with different optional parameters one can use it to count the number of lines, words, and characters in a text file.

How do I count the number of lines in a file in Terminal?

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.

Which command used to count number of lines in a file?

Use the wc command to count the number of lines, words, and bytes in the files specified by the File parameter.


2 Answers

Pipe the result to wc using the -l (line count) switch:

grep -Rl "curl" ./ | wc -l 
like image 80
João Silva Avatar answered Dec 08 '22 06:12

João Silva


Putting the comment of EaterOfCode here as an answer.

grep itself also has the -c flag which just returns the count

So the command and output could look like this.

$ grep -Rl "curl" ./ -c 24 

EDIT:

Although this answer might be shorter and thus might seem better than the accepted answer (that is using wc). I do not agree with this anymore. I feel like remembering that you can count lines by piping to wc -l is much more useful as you can use it with other programs than grep as well.

like image 22
JelteF Avatar answered Dec 08 '22 07:12

JelteF