Unix: list out Pattern occurrence (count)

Need to search a pattern occurrence (count) in a specified file.
Below is the details

$ cat fruits
apple apple
ball ball
apple
ball ball ball
apple apple apple
cat cat
cat cat cat
apple
apple
Note: If I'll use the grep command with -c option then it'll count the 1st occurrence in the lines but not the repeated occurrence like

$ grep -ic apple fruits
5
but here apple is occurring 8 times.

Perhaps you could translate all whitespace characters to newlines before piping into grep.

Regards,
Alister

Could you please give me an example

cat file | tr " " "\n"
tr " " "\n" < file
 
$ nawk '{for(i=1;i<=NF;i++)if($i == "apple") ++count;} END{print count}' test
8
$ cat test
apple apple
ball ball
apple
ball ball ball
apple apple apple
cat cat
cat cat cat
apple
apple
perl5.10.1 -0777 -nE 'say scalar grep {/\bapple\b/} split' INPUTFILE
8

I love perl. :slight_smile: