W.A.S.S to count the number of files which contains a given pattern and also display the names of all those files.
echo -e "Enter the pattern : \c"
read ptrn
cnt=0
for i in `ls -1`
do
if [ -f $i ]
then
l=`grep -E $ptrn $i|wc -l`
if [ $l -gt 0 ]
then
echo "$ptrn is found in $i file."
cnt=`expr $cnt + 1`
fi
fi
done
echo "$ptrn is found in Total $cnt file(s)."
Output
sh filept.sh
Enter the pattern : narendra
narendra is found in temp3.txt file.
narendra is found in temp4.txt file.
narendra is found in Total 2 file(s).