how to find the exact pattern from a file?

Hi
Let say there is a file a.txt which contain number rows.
My intention is to find the number of occurences of a pattern. Let say the pattern is mdbase. then it should not count the occurences of mdbase1 or mdbase2 like this. When I tried to find it like
grep "/backup/surjya/mdbase" xmldir.conf_backup | wc -w
The out put is 3 rather than 1. So please let me know how it can be resolved

Thanks

If mdbase is the end of the field or word separated from whatever follows by whitespace you can use:-

grep "/backup/surjya/mdbase\>" xmldir.conf_backup | wc -w

Without the wc

grep -c "/backup/surjya/mdbase\>" xmldir.conf_backup

try this
grep "^pattern$" file |wc -w

I tried like below:
dir2=\"$dir1"\>"\"
echo $dir2
stgdircount=`grep $dir2 xmldir.conf | wc -w | awk '{print $1}'`
echo $stgdircount

let say dir1=/backup/surjya/mdbase. hence the output for dir2 is
"/backup/surjya/mdbase\>"
and for stgdircount is 0, but exactly there is two entries in this file. Please give me some idea.

In the grep statment you gave, it is searching for

"/backup/surjya/mdbase\>"

I am sure you want to search for

/backup/surjya/mdbase and not the above statement.

Look at this

sh-2.05b$ cat surya.txt 
/backup/surjya/mdbasewed
/backup/surjya/mdbase2
/backup/surjya/mdbase
/backup/surjya/mdbase3
/backup/surjya/mdbase

sh-2.05b$ grep -c "/backup/surjya/mdbase\>" surya.txt 
2
sh-2.05b$ grep -c "/backup/surjya/mdbase" surya.txt 
5
 

yes you are right but why it gives 0 when I try to execute it through shell script. Let say the script is
dir1=/backup/surjya/mdbase
stgcnt=`grep -c $dir1 surya.txt `
echo $stgcnt
The value of stgcnt is 5.
Just modify the script like

dir1=/backup/surjya/mdbase
stgcnt=`grep -c \"$dir1"\>"\" surya.txt `
echo $stgcnt
The value of stgcnt is 0
I hope my question is clear.

Why are you gold-plating that search statement ? It is as simple as

sh-2.05b$ cat surya.sh 
#! /bin/sh
dir1=/backup/surjya/mdbase
stgcnt=$(grep -c "$dir1\>" surya.txt)
echo $stgcnt
sh-2.05b$ ./surya.sh 
2
sh-2.05b$