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.

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.

  1. How to count the number of duplicated lines in a file
  2. Count the total number of logged in user
  3. Count the total number of objects in the collection in MongoDB
  4. How to count the number of line in a file
  5. How to use sed to simulate grep -v?

Leave a Reply

(required)

(required)

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>