LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 11-29-2012, 12:38 AM   #1
Arpitha10
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Rep: Reputation: Disabled
How to grep for an exact word?


I have a word hello in a line and hellos in another. I need to print the line that contains hello and not hellos. How do i do that. I haev tried -w -x -F everything. Nothing works.
 
Old 11-29-2012, 12:41 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
Show example data and really you should have started a new thread...
 
Old 11-29-2012, 12:57 AM   #3
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
Hey Arpitha10,

$cat file1
hello
hellos

$$ grep 'hello\ ' file1
hello

**Note: \ is escaping character.
 
Old 11-29-2012, 02:00 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,778
Blog Entries: 4

Rep: Reputation: 282Reputation: 282Reputation: 282
The option -w should work:
Code:
grep -w "hello" file1
I tested it at my side and it's working fine.

Last edited by shivaa; 11-29-2012 at 02:02 AM.
 
Old 11-29-2012, 02:39 AM   #5
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,001

Rep: Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003
you can also try \b:
grep "\bhello\b" file

grep 'hello\ ' file will not work at the end of line

which version of grep did you try?
 
1 members found this post helpful.
Old 11-30-2012, 08:34 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
As listed in the grep man page, and in more detail in the info page, \b matches the empty space between word characters (note that a "word" character is "a-zA-Z0-9" and the underscore; I believe it's also locale dependent), including the ones at the ends of the lines There are also \< and \> which match only the transition before or after a word character, and \B, which matches the space between two word characters.

Code:
grep "\<hello\>" file   #matches only the whole word "hello"
grep "\<hello\B" file	#matches words like "hellos", but not "hello"
 
Old 12-11-2012, 04:22 AM   #7
Arpitha10
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Smile

thanks all!! It has been really very helpful..
 
Old 12-11-2012, 05:25 AM   #8
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,001

Rep: Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003
glad to help you. If you really want to say thanks just press YES (bottom right corner)
 
1 members found this post helpful.
Old 12-16-2012, 06:23 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946


I just came back after 10 days, and I see that I made a significant error in my previous post. It looks like the time allowed for editing it directly has passed, so here's the correction:

Quote:
\b matches the empty space between word characters a word character and a non-word character...
In addition, while the explanation for \B is correct as far as it goes, it is also incomplete. It matches the space between two word characters, or between two non-word characters.

In other words, \b matches if there's a transition between word and non-word, and \B matches if there's no transition.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] delete lines that contain an EXACT word DarkLight90 Linux - Newbie 7 06-28-2012 09:52 AM
[SOLVED] Remove exact matching word using sed kushalkoolwal Programming 2 08-20-2009 05:15 PM
command for grep the exact word nagendrar Linux - Newbie 7 04-29-2009 02:33 PM
grep the exact string only ZAMO Linux - General 11 08-28-2008 05:08 AM
grep exact letters packets Linux - Newbie 1 11-30-2007 08:39 PM


All times are GMT -5. The time now is 06:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration