How to count total number of word occurrences using grep on Linux or Unix

www.la‮ttu‬uri.com
How to count total number of word occurrences using grep on Linux or Unix

To count the total number of occurrences of a word in a file using grep on Linux or Unix, you can use the -o option to print only the matched (non-empty) parts of a matching line, and then use the wc command to count the number of lines.

For example, to count the total number of occurrences of the word "word" in the file file.txt, you can use the following command:

grep -o "word" file.txt | wc -l

This will print the total number of occurrences of the word "word" in the file.

You can also use the -c option with grep to count the number of lines that contain the word. For example:

grep -c "word" file.txt

This will print the total number of lines in the file that contain the word "word".

You can use the -i option with grep to perform a case-insensitive search. For example:

grep -ci "word" file.txt

This will count the total number of occurrences of the word "word", regardless of whether it is in upper or lower case.

Created Time:2017-10-28 21:39:01  Author:lautturi