LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 08-27-2008, 02:04 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 579

Rep: Reputation: 30
grep the exact string only


Hi all,

When I try to grep a particular string, as usual am getting all the words matching the string. How can I grep the words matching the exact sting only in a file.


Thanks
 
Old 08-27-2008, 02:10 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Did you read the grep manual?
Code:
       -F, --fixed-strings
              Interpret PATTERN as a list of fixed strings, separated by newlines,
              any of  which  is  to  be matched.
Yves.
 
Old 08-27-2008, 02:25 AM   #3
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 38
If you're grepping for a word, you can also use grep -w <word> <file>.
 
Old 08-27-2008, 02:46 AM   #4
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 579

Original Poster
Rep: Reputation: 30
Yes I read the grep manual and I am unable to locate the exact switch for grep which i need. Both the option you gave is not working in my case.


To explain myself better, I have lines in a file which consists of "samba","samba-3.0.25b-0.4E.6" "samba-common-3.0.25b-0.4E.6" like this.(they are not in the same line)


I want to grep the line which has the string "samba" only .
The string "samba" is not a separate line ...It is a word in a line .

Last edited by ZAMO; 08-27-2008 at 02:57 AM.
 
Old 08-27-2008, 02:55 AM   #5
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Do you mean not the samba-common line? If so, will the form always be samba-X where X is a numeric digit 0-9?
 
Old 08-27-2008, 03:02 AM   #6
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 579

Original Poster
Rep: Reputation: 30
No , I don't mean the samba-common line.
grep samba equals grep samba[*].

I want to get the line which has only samba and not any other. Am using a second and third grep with -v to ignore other options like "samba-3.0.25b-0.4E.6" "samba-common-3.0.25b-0.4E.6"

Let me know , Is there a way to get the line with a particular string , without pre or post string occurred lines.

Last edited by ZAMO; 08-27-2008 at 03:04 AM.
 
Old 08-27-2008, 03:23 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
If samba by itself is followed by a space or newline, you could check for that.
echo -e "samba\nsamba-common\nsamba-server" | grep -e "samba$" -e "samba "

If you use the -v option, you could elimate the other samba entries with:
grep samba file | grep -v -e samba-3.0.25b -e samba-common

You can't both match a pattern and reject a pattern by mixing -e and then -v. You can use the -v option and include multiple terms with the -e option.
 
Old 08-27-2008, 04:57 AM   #8
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,009

Rep: Reputation: 171Reputation: 171
Is this what you're trying to do?

Code:
mike@continuity:/tmp$ grep samba sambalines
samba
samba-3.0.25b-0.4E.6
samba-common-3.0.25b-0.4E.6
mike@continuity:/tmp$ grep ^samba$ sambalines
samba
mike@continuity:/tmp
 
Old 08-27-2008, 07:26 AM   #9
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
You then need the -x switch.

Yves.
 
Old 08-27-2008, 10:17 AM   #10
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,009

Rep: Reputation: 171Reputation: 171
Quote:
Originally Posted by theYinYeti View Post
You then need the -x switch.
I didn't realise grep had such a switch. Much better than what I suggested.
Code:
mike@continuity:/tmp$ grep samba sambalines
samba
samba-3.0.25b-0.4E.6
samba-common-3.0.25b-0.4E.6
mike@continuity:/tmp$ grep -x samba sambalines
samba
mike@continuity:/tmp$
 
Old 08-28-2008, 04:47 AM   #11
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 579

Original Poster
Rep: Reputation: 30
Thanks Your suggestions and help.

Arizonagroovejet&theYenyeti,

You are logically correct. But my string is inside a long sentence which made up of words and special characters. So it was not working. Its working on the string , where it is a separate word.

I went to grep like this finallywhat I told in my previous post for -v)

grep samba sambalines |grep -v samba-3.0.25b-0.4E.6 -e samba-common

Thanks again to All.
 
Old 08-28-2008, 05:08 AM   #12
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,009

Rep: Reputation: 171Reputation: 171
Oh, didn't read your previous post properly. But you seem to have contradicted yourself by providing the example lines of the files as being
Quote:
Originally Posted by ZAMO
To explain myself better, I have lines in a file which consists of "samba","samba-3.0.25b-0.4E.6" "samba-common-3.0.25b-0.4E.6" like this.(they are not in the same line)
and then

Quote:
Originally Posted by ZAMO
The string "samba" is not a separate line ...It is a word in a line .
So you give an example where samba is the only word on the line and then say that it isn't. Then in a later post you say
Quote:
Originally Posted by ZAMO
I want to get the line which has only samba and not any other.
Which to me means samba is on a line by itself which is what you implied originally.

So I'm a little confused as to what you want now. But if you want to find lines where samba appears as a distinct string amongst others and not as part of a longer string then why not just grep for samba preceded and followed by a space?

Code:
mike@continuity:/tmp$ cat sambalines
samba
samba-3.0.25b-0.4E.6
samba-common-3.0.25b-0.4E.6
blah samba blah blah
mike@continuity:/tmp$ grep " samba " sambalines
blah samba blah blah
mike@continuity:/tmp$
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep question - match exact string Panagiotis_IOA Linux - General 2 01-20-2014 04:34 AM
grep exact letters packets Linux - Newbie 1 11-30-2007 08:39 PM
grep [exact phrase] chrisfirestar Linux - General 2 02-09-2004 04:30 AM
how to grep only one string pr match gummimann Linux - General 3 11-06-2003 09:40 AM
grep - finding string and replacing with new ckibler Linux - Newbie 6 08-01-2003 06:25 PM


All times are GMT -5. The time now is 12:35 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