Re: tell me how many specific words there are in the file?



Måns Rullgård wrote:
Pascal Bourguignon <usenet@xxxxxxxxxxxxxxxxx> writes:

nitrogenix.fusion@xxxxxxxxx writes:

I'm trying to get the user to enter a word, then the script should
search for how many of that word exists in the file. For example, I
want it to say, "There are 7 'and' words in the file", or "There are 5
'the' words in the file" etc...

I have the following which I used to find a specific character, but I
need to change this round to find words instead.

tr -dc $char < text | wc -c

Thanks for your help.
man grep
man 7 regex

Off the top of my head, I can't think of a grep command that will
count multiple matches on the same line. Am I overlooking something?

Nope, I think you're right. But there is an easiy enough solution:

fmt -1 "$file" | grep -cw "$word"

I know, it's a dirty solution, but it works. Assuming that "fmt" and
you agree on what makes a word boundary.

- Logan
.