While grep is most often used for finding strings and neighboring text, it can be used for counting lines and occurrences as well.
grep count lines
To count the number of lines that a string appears in using grep:
$ grep -c “string”
Run in Warp
For which the -c flag is used to count the number of lines that are matched and print out the number. This can be useful, for example, when you want to search through log files for the number of entries from a particular IP, endpoint or other identifier where you only care about the number of lines, not the full number of matches.
grep count occurrences
If however you want to count the number of occurrences of a string, beyond simply the number of lines, then the command can be used:
$ grep -o “string” | wc -l
Run in Warp
For which, the -o flag gets the occurrence of that string, while wc -l will count the number of times the occurrence appears on each line. This is useful when you want to know how many times a particular string occurs in a document such as the number of times a name, variable or IP address appears.
grep print occurrences
This command is simplified if you only want to print out the occurrences of that string rather than count the number of occurrences. This is done using:
$ grep -o “string”
Run in Warp
For which the -o flag is used to print out the matches on each line. This is useful when you want to see the context of the matches in the text they are part of, such as what strings the matches end up as.
Common “gotchas” when using grep to count
grep uses regex standards
It is important to know that the “string” following the grep command will match the document based on regular expression standards. This means that simply typing in test will match on longer strings containing that word such as “testing”. To match only the specific word use regex expressions or the -w flag. For example:
$ grep -o “\btest\b” |wc -l
$ grep -ow “test” | wc -l
Run in Warp
grep counting multiple words
$ grep -o -E “string1|string2” | sort | uniq -c
Run in Warp
If you want to search for multiple words at the same time, this command becomes more complicated. To print out the word and count you can use:
Read more on grep multiple strings to understand how the “or” part of this command came together.
grep counting across multiple files
To match across multiple files and count the occurrences then this can become even more complicated. But the following command should print out the occurrences and which file they occur in:
$ grep -0 “string” | cut -d ‘:’ -f 1 | uniq -c
Run in Warp
All of these commands can also be combined with the -i flag so that the string match is case insensitive
Use awk or sed for text manipulation
If you want to manipulate text, or work with specific fields of a file, you will probably want to use a more specific tool such as sed or awk
Find out more about grep
As always if you want to find out more about how to use the grep tool you can use:
$ man grep
Run in Warp
Which will print out all the options with explanations. Or:
$ grep --help
Run in Warp
Which will print out a short page of all the available options.
Written by
Philip Wilkinson
Software Engineer, Amazon
Filed Under
Related Articles
Grep Multiple Strings
How to filter lines and extract specific information from the output of commands or text files based on string patterns and regular expressions with grep.
How To Filter The Output of Commands
Learn how to filter and format the output of commands and logs using the grep, awk, uniq, head, and tail commands.
How to Make Grep Case Insensitive
By default, grep is case sensitive
Grep Across Multiple Lines
Guide on several cases of using grep across multiple lines
Grep In a Directory
Learn how to use grep to search for words and phrases within a directory and all its subdirectories, a specific directory, all files, and other variations.
Exclude With Grep
Excluding unwanted key terms or directories when using grep