total count of a word in the files

Hi Friends,
Need help regarding counting the word "friend" in files test1.txt and test2.txt. ( there is no gap/space between word )

cat test1.txt
himynameisrajandiamfriendofrajeshfriend
wouldyouliketobemyfriend.
 
cat test2.txt
himynameisdostandiamfriendofdostfriend
wouldyouliketobemyfriend.

ouput is:
total number of occurance of friend is: 6

i tried writing a shell script, but it is not working when there is no gap.
Below is the code

for i in `cat test[1-2].txt`; 
do
if [ "$i" = "friend" ]; then
cnt=`expr $cnt + 1`
fi
done
echo " total number of occurance of friend is: $cnt "

help needed.

awk '{n+=gsub("friend",x)}END{print n}' test1.txt test.txt
1 Like

one liner...

grep -oi "friend" (filesname)  | wc -l