The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > OS Specific Forums > Linux
Google UNIX.COM


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to grep for a word and display only the word ananthmm UNIX for Dummies Questions & Answers 6 05-29-2008 05:00 AM
grep for "exact word" bullz26 Shell Programming and Scripting 7 03-14-2008 02:00 AM
Exact Match thru grep ????? manas_ranjan UNIX for Advanced & Expert Users 2 08-17-2007 02:57 AM
How to grep the exact name? csaha Shell Programming and Scripting 3 01-31-2006 08:47 AM
grep - exact wxornot UNIX for Dummies Questions & Answers 9 01-02-2006 11:03 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-16-2008
Registered User
 

Join Date: Jul 2007
Posts: 1
Smile 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
Reply With Quote
Forum Sponsor
  #2  
Old 06-16-2008
Registered User
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 352
check if -w option is enabled on your system

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\>"
Reply With Quote
  #3  
Old 06-16-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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?

Code:
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.
Reply With Quote
  #4  
Old 06-17-2008
joeyg's Avatar
Moderator
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 984
Wink Since it is at the beginning of the line...

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

Two examples:
Code:
> 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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0