Counting lines starting with a specific word

How to count the number of lines in a text file starting with a specific word?

I do not want to use sed and then wc -l . Any better solution?

+7
linux count
source share
3 answers

Try the following: -

  awk '/^yourwordtofind/{a++}END{print a}' file 
+7
source share

Just flatten your word and then use wc -l to count the lines ... like this grep '^your_word' /path/to/file | wc -l grep '^your_word' /path/to/file | wc -l

+10
source share
 grep -c "pattern" <filename> 

For example: if you want to find the more template in the test.txt file, then the command is given below:

 grep -c "more" test.txt 
+4
source share

All Articles