Counting occurences of a words/pattern using grep and wc
Some times you will come across the requirement of counting how many times a word/pattern has occurred in a file (text).
Here is a simple usage of the command grep and wc to do the same.
lets use the following file for example. (I have named it sample.txt)
Linux is a nice operating system.
Many people thing Linux just has a text based interface.
When people see the GUI on Linux they are really amazed.
Linux was developed by Linus Torvalds.
Linux is to UNIX, so if you have worked on Linux you will be able to work on UNIX also.
Now to count how many time “Linux” occurred in the text you can use:
grep -o ‘Linux’ sample.txt | wc -l
Explaination:
The grep command with -o option searches for the pattern in quotes, in this case ‘Linux’, If you just run the command like that you will get an output like
grep -o ‘Linux’ sample.txt
Linux
Linux
Linux
Linux
Linux
Linux
Now in the actual command
grep -o ‘Linux’ sample.txt | wc -l
When this output is piped to wc -l, which is a word counter tool, with the option -l it counts the number of lines that it receives, and since the output of grep gives each match in a different line, the number of lines are equal to number of occurrences of the text/pattern/word.
Remember grep is case sensitive so use the -i option to ignore case
grep -io ‘Linux’ sample.txt | wc -l
else only ‘Linux’ will be selected all other occurrences will be ignored.
Of course if you are just looking for how many lines have a particular pattern/word occurring and not the count of the word/pattern it self use
grep -c ‘Linux’ sample.txt
OR
grep -ic ‘Linux’ sample.txt // to ignore case
Of course if you are familiar with patterns matching, you can replace ‘Linux’ with your regular expression to look for occurrences of a particular pattern
e.g ‘(Linux|UNIX|AIX)’ will look for occurrence of Linux or UNIX or AIX.
Note if you are going to use grep do not forge to escape the brackets and the pipe symbol.
Hope that was useful.
NOTE: This explanation is with respect to BASH Shell (GNU bash, version 3.1.17(2)) with grep (GNU grep) 2.5
[end]




















