option of grep for counting exact word ??

Hi All,
I have a quary regarding grep command in linux.
I have a file which contains

56677
56677
+56677
+56677
56677
56677
56677

I want to extract total count of "56677"
When I hit the following command

#cat filename | grep -w -c '56677'

the result comes 7. Its counting including "+56677". I need count of only "56677" .

Is there any option of grep to extract count of only "56677" excluding count of "+56677".

Please provide the command ASAP.
Waiting for the response ...
Thanks in advance ..

Maddy

Check if you can use -w option in the grep. This would depend on what system you are using.

Sometimes exact word can be searched as putting the value in angled brackets.

grep "\<VALUE\>"

I don't think there is an option in grep itself to fiddle with the semantics of the -w option. Some locales probably have slightly different definition of what constitutes a "word" but relying on that seems brittle at best. Perhaps it's simplest to explicitly specify what characters are allowed as word separators. Something like this, maybe?

egrep -c '(^|[ 	])56677([ 	]|$)' filename

The stuff between the square brackets are a space and a tab. (In some shells you need to type something like ctrl-v tab to enter a literal tab character.) Add more characters if you want other characters to be allowed as word separators. The regular expression means "beginning of line, or one of the characters between the square brackets", followed by your string (in this case 56677), followed by "one of the characters between the square brackets, or end of line". Plain grep does not understand this syntax; it's "extended regular expressions", hence, "extended grep" aka egrep. (Though POSIX specifies a way to use a similar set of operators with plain grep, too, I believe).

Note also that you don't need or want the cat there; grep can read what input files you want to feed it all by itself.

why not use the 'tag' to work from the beginning of the line?

Two examples:

> grep -c "^56677" <sample
5
> cat sample | grep -c "^56677"
5

The second example is the more intuitive way of thinking through the process: display the file and then specify what is being selected. The first is the more 'perfect' unix programming solution since grep does not need a cat command - in english, select for something taking a file as input.