The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Operating Systems > 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 08:00 AM
grep for "exact word" bullz26 Shell Programming and Scripting 7 03-14-2008 05:00 AM
Exact Match thru grep ????? manas_ranjan UNIX for Advanced & Expert Users 2 08-17-2007 05:57 AM
How to grep the exact name? csaha Shell Programming and Scripting 3 01-31-2006 11:47 AM
grep - exact wxornot UNIX for Dummies Questions & Answers 9 01-02-2006 02:03 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-17-2008
maddy maddy is offline
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
  #2 (permalink)  
Old 06-17-2008
nua7 nua7 is offline
Registered User
  
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 353
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\>"
  #3 (permalink)  
Old 06-17-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
  #4 (permalink)  
Old 06-17-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
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.
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0