How to count the pattern in a file by awk

hello everybody,
I have 3 files
eg-

     sample1
     sample2 
     sample3

each file contain word babu many times

eg-

cat sample1
babu amit msdfmdfkl babu abhi
babu ruby 
amit babu

I want to count only the count of babu ,how many times it appeared .
any help will be appreciated.....................

For a single file:

$> cat infile
lala baba lalala baba s
aaj baba
baba sazh23r baba 09q8wfh2983f baba
$> awk '{for(x=1; x<=NF;x++){if($x ~ /baba/){z++}}} END{print z}' infile
6

If you want to count it for more files you can just add the filenames and get a sum of all matches.

cat /tmp/t1
one
two
two
one
three

sort t1 | uniq -c  or to take it even further sort t1 | uniq -c | grep two
sed -n 's/\(babu\)/\1\n/pg' file1 file2 file3 | grep babu|wc -l

In case this fails for someone, it's probably due to \n in the replacement text being a gnu extension.

---------- Post updated at 03:42 PM ---------- Previous update was at 03:40 PM ----------

Another awk alternative:

awk -F babu 'NF{x+=NF-1} END {print x}' file

Note: babubabu would count as two instances of babu instead of being ignored as a different word.

thanks a lot !!!!!! alister