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


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

There are total 4 lines match the word foo.

 Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>