Need script to count specific word and iterate over number of files.

Hi Experts,

I want to know the count of specific word in a file. I have almost 600+ files.

So I want to loop thru each file and get the count of the specific word.

Please help me on achieving this...

Many thanks

This will check all files in the current directory. The sed is to add a newline after the word, to guarantee it never happens more than once per line, so that grep can count them properly. If you know the word will never happen more than once per line, the sed part can be dispensed with.

find ./ -type f |
while read FILENAME
do
        echo -n "${FILENAME}:"
        sed 's/word/word\n/g' < "${FILENAME}" | egrep --count word
done

should work if the word is unique enough, never occuring in a piece of a larger word.

1 Like

its not working.. :frowning:
files are being printed but there is no count for my specific word.