Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting lines starting with a certain word

Tags:

linux

count

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

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

like image 237
user2517676 Avatar asked Aug 28 '13 13:08

user2517676


People also ask

How do I count the number of lines in a specific word in Linux?

How do I count lines if given word or string matches for each input file under Linux or UNIX-like operating systems? You need to pass the -c or --count option to suppress normal output. It will display a count of matching lines for each input file.

How do I count lines using grep?

Using grep -c alone will count the number of lines that contain the matching word instead of the number of total matches. The -o option is what tells grep to output each match in a unique line and then wc -l tells wc to count the number of lines. This is how the total number of matching words is deduced.

Which of these commands would you use to count the number of lines containing a word or phrase?

The wc command stands for “word count” and has a quite simple syntax. It allows you to count the number of lines, words, bytes, and characters in one or multiple text files.

Which of these commands would you use to count the number of lines containing a word or phrase grep C grep I grep N grep V?

This uses the -o option of grep, which prints every match in a separate line (and nothing else) and then wc -l to count the lines.


1 Answers

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

like image 176
dostrander Avatar answered Oct 15 '22 07:10

dostrander