6

Lets take the below content as an example

    This file is a test file 
    this file is used to count the word 'file' in this test file
    there are multiple occurrences of word file in some lines in this test file

I want to count the word 'file' in the above content.

I'm using the below shell command

   cat $filename |  sed "s/_/new/g" | sed "s/$word/_/g" | tr -c -d _ |wc -c

Is that ok or any better ideas ..?

William Pursell
  • 204,365
  • 48
  • 270
  • 300
Manikandaraj Srinivasan
  • 3,557
  • 5
  • 35
  • 62

12 Answers12

9

Using tr for separating words and then grep and wc seems possible :

tr -s ' ' '\n' < file.txt | grep file | wc -l
Nibbler
  • 503
  • 5
  • 10
7
grep -cow "$word" "$filename"

The -c option specifies to report a count.

The -o option specifies to count each occurrence, not just the number of matching lines.

The -w option specifies to count word matches only, i.e. not partial matches such as "files" or "profiles".

Unfortunately, some versions of grep do not work correctly when you combine -c and -o. If you have that bug, @Nykakin's answer is a good workaround.

Pay attention to the proper quoting of interpolated variables, also.

tripleee
  • 175,061
  • 34
  • 275
  • 318
6
grep $word $filename -o | wc -l
Nykakin
  • 8,657
  • 2
  • 29
  • 42
1

I would recommend the easiest method here which will be:

grep -c "file" filename

I you wish you strictly search for that word and no prefix and suffix then modify it as follows:

grep -wc "file" filename
Preeti Maurya
  • 431
  • 1
  • 7
  • 17
  • This counts the number of lines which contain the word at least once, not the number of actual occurrences of the word. – tripleee Jul 18 '17 at 12:31
0
cat $filename | tr -s ' ' '\n' | grep -c $word
jassinm
  • 7,323
  • 3
  • 33
  • 42
0

You could do it all in awk or perl and you can definitely remove the cat (sed can work on filenames too). grep by itself is a no-go, since it will only count one match per line.

$ sed "s/_/new/g" delmememetest | sed "s/$word/_/g" | tr -c -d _ |wc -c
7
$ grep -c file delmememetest
3

Let's try another funky approach, to make grep useful:

$ sed "s/${word:0:1}/\n&/g" delmememetest | grep -c "$word"
7

I insert a newline before each character that is the same as the first character of the search word. That way only one match per line does not interfere with the counting. If you have a recent version of GNU grep, the -o option used in another answer will ensure the same.

In any case, make sure the pattern you match against is not just $word or words with the same root will match too (or use the -w switch).

lynxlynxlynx
  • 1,371
  • 17
  • 26
0

Some of the voted solutions using the tr command couldn't handle the situation where there's linked word like "filefile". Here is my solution using Perl:

perl -p -e s/file/file\\n/g $filename | grep -c file

The -p tells perl to run a loop and to echo the output. The -e specifies that the one-line program is coming next.

HZhang
  • 175
  • 12
0

I found this to be the easiest way:

 grep -o "$word" "$file" | wc -w

The -o option in grep specifies to count each occurrence, not just the number of matching lines.

The -w option in wc is to count only the whole words.

Siddharth Dushantha
  • 1,391
  • 11
  • 28
0

This should work every time:

#!/bin/sh

echo "Enter the term"

read term

result=`grep -o $term file.txt | wc -l`

echo $result
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
animorph
  • 11
  • 2
  • No, this breaks if `term` contains whitespace or shell metacharacters. See [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) The [useless `echo`](https://www.iki.fi/era/unix/award.html#echo) is also mildly unsettling. – tripleee Oct 09 '22 at 19:07
-1

...I like to keep it simple:

grep $string /file/name |wc -l

or

cat /file/name |grep $string |wc -l
Pierre-Luc Pineault
  • 8,993
  • 6
  • 40
  • 55
  • The above answer may not work, person wants to know the count of word. what you are giving is effectively the number of lines where the word has been found – Ajay Dec 22 '15 at 10:24
  • In addition to other bugs in several answers here, this suffers from [incorrect quoting](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable). It's also hard to see how [using a useless `cat`](https://stackoverflow.com/questions/11710552/useless-use-of-cat) is simpler than not using it. – tripleee Oct 09 '22 at 19:06
-1

Use the following command :- less fileName | grep wordToBeSearched | wc -l Here less is the type of editor you want to use If you wish to use nano editor, then use the following command :- nano fileName | grep wordToBeSearched | wc -l Here wc stands for word count and -l for the number of lines having this word.

Dhumil Agarwal
  • 856
  • 2
  • 11
  • 19
-2

The code:

   count=0;
    for i in `cat $filename`;
        do if [ $i == "file" ];
    then ((count++))fi $i; 
    done;
    echo $count;
One Man Crew
  • 9,420
  • 2
  • 42
  • 51