how many times a word occur in afile

i want a shell script program for how many times a word occur in a file.
i need not the line number but i want the counts of the particular word
for eg:-
hai how r u..
i am from andhra pradesh..
i am from tenali.i need this answer.i need it urgently..
i hope u will answer this ...
this is the matter in a file..i want to count the word "i" here..
my answer is i-11........
like this give a shell script program

check this

grep -w "echo" tst | wc -w

echo is the word you are looking for and tst is the file.

it not gives perfect count.......

grep just reports the number of lines which match (and you should use grep -c rather than pipe to wc).

If you have grep -o then you can use that:

grep -w -o echo tst | wc -l

In your example you seem to want to count the word-internal occurrences as well; take out the -w option in that case.

Please use code tags to make your examples clearer.

thanks ....its working.....

i need a shell script progrm...to extract a sub-string from a string...
foe eg:-
my string is madhu
i want to extract sub-string from this .my sub string is "mad"..
i want the out put will be a sub string..
0/p is mad......
pls give this answer..

mad from madhu....extracting substring from a string

Shouldn't that be as

grep -cw -o echo tst 

At least with GNU grep, -c and -o doesn't work as you'd hope; you still get the count of matching lines, not occurrences. Maybe somebody should file a bug report, though.

madhu.it: Please start a new thread for a new question.

Oh yes,

thats a problem with GNU grep ( 2.5.1 )