Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Mar 082010
 

Count the number of matching lines from grep

Answer:

grep command is useful for searching file's content by a key string, e.g.

# grep foo text.txt

All the lines matching foo will be printed.

However, to only print the number of matching lines, you can use the -c flag

# grep -c foo text.txt
4

There are total 4 lines match the word foo.