My go to way of counting the number of matches in a grep of a file is to pipe it to another command β wc.
grep
wc
Here is what that looks like with the README for this repoarrow-up-right. This counts the number of lines that start with ###.
###
$ grep '^###' README.md | wc -l 48
When wc is used with the -l flag, it gives a count of the number of lines. In this case the number of grep matches that get piped to it.
-l
There is another way to do this solely with the grep command β using the -c flag.
-c
$ grep -c '^###' README.md 48
When you include the -c (or --count) flag with grep, instead of the matches being output, the count of the matches is output.
--count
See man grep for more details.
man grep
Last updated 4 years ago