Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to grep number of occurrence of two different words e.g. 'register' and 'evn' in a file on Linux ?

The output should be like following:

share|improve this question
You should clarify the requirements. 1) Multiple occurrences of a word on the same line should be counted as 1 (as in McNisse's answer) or should count each (as in my answer)? 2) All words with the same base should be counted (as in dchirikov's answer) or only exact matches (as in my answer). – manatwork Jan 9 '13 at 11:37

Use awk

awk '/register/ {r++} /evn/ {e++} END {printf("register:%d\nevn:%d\n", r, e)}' /path/to/file 
share|improve this answer

In case reversed output format (count first, word after) is also acceptable, this does it too and is easy to add more words:

tr -c '[:alpha:]' '\n' < /path/to/file | sort | uniq -c | grep -w 'register\|evn'
  • Counts each word occurrence, even if there are multiple occurrences in the same line.
  • Counts exact matches of the words, not including the suffixed variants.
share|improve this answer

You can calculate it separately:

$ word=register; count=`grep -o $word /path/to/file| wc -l`; echo $word:$count
$ word=evn; count=`grep -o $word /path/to/file| wc -l`; echo $word:$count
share|improve this answer
You don't need to wc -l. grep -c gives the count directly. – McNisse Jan 9 '13 at 11:35
echo $word:$( grep -wc $word /path/to/file )

Works with Bash/Ksh and GNU grep

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.