'Shell Script to Count the Occurrence of a Word in a file
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 ..?
Solution 1:[1]
grep $word $filename -o | wc -l
Solution 2:[2]
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.
Solution 3:[3]
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
Solution 4:[4]
cat $filename | tr -s ' ' '\n' | grep -c $word
Solution 5:[5]
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).
Solution 6:[6]
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.
Solution 7:[7]
...I like to keep it simple:
grep $string /file/name |wc -l
or
cat /file/name |grep $string |wc -l
Solution 8:[8]
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.
Solution 9:[9]
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.
Solution 10:[10]
This should work every time:
#!/bin/sh
echo "Enter the term"
read term
result=`grep -o $term file.txt | wc -l`
echo $result
Solution 11:[11]
The code:
count=0;
for i in `cat $filename`;
do if [ $i == "file" ];
then ((count++))fi $i;
done;
echo $count;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
