LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-11-2010, 02:40 AM   #1
andy2008
Member
 
Registered: Nov 2010
Posts: 40

Rep: Reputation: 1
Using grep to find EXACT MATCH


Hi guys, I'm trying to find exact matches of some users in the /etc/passwd file using "grep -w", but it doesn't always work.
For example, I have the following users:


john.stewart:x:579:579::/home/john.stewart:/bin/bash
andy.stewart:x:580:580::/home/andy.stewart:/bin/bash


So, let's say, I want to search for the user "stewart" (which doesn't exist)

grep -w "stewart" /etc/passwd

I get:

john.stewart:x:579:579::/home/john.stewart:/bin/bash
andy.stewart:x:580:580::/home/andy.stewart:/bin/bash


Is there any way to match the EXACT usernames even though there are other partial matches that include underscores, dots, etc?
 
Old 11-11-2010, 02:52 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,128

Rep: Reputation: 986Reputation: 986Reputation: 986Reputation: 986Reputation: 986Reputation: 986Reputation: 986Reputation: 986
Actually I've yet to see a case where it doesn't work - as defined.
Proper specification of the regex usually works just fine.
 
Old 11-11-2010, 04:37 AM   #3
andy2008
Member
 
Registered: Nov 2010
Posts: 40

Original Poster
Rep: Reputation: 1
I tried everything... it doesn't work.

I tried: grep -w "^john.stewart:" /etc/passwd to match all lines starting with john.stewart followed by colon... I have no idea why, but it doesn't work... it doesn't find anything, although it should. Any suggestions?
 
Old 11-11-2010, 04:49 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,128

Rep: Reputation: 986Reputation: 986Reputation: 986Reputation: 986Reputation: 986Reputation: 986Reputation: 986Reputation: 986
Quote:
Originally Posted by andy2008 View Post
I tried everything... it doesn't work.
I would suggest you read the manpage for grep and see how they define word bounding.

regex is a slippery subject - as I am wont to say "regex ain't regex".
 
Old 11-11-2010, 05:12 AM   #5
andy2008
Member
 
Registered: Nov 2010
Posts: 40

Original Poster
Rep: Reputation: 1
Oh, very helpful! Gee, now why didn't I think of that? Oh, wait, as a matter of fact, that's the first thing I did, but it doesn't seem like it helped, does it? You know, since I'm on this FORUM asking for HELP!

Listen, syg00, some people, like you, don't seem to grasp the meaning of the word FORUM. Let me explain, it's easy: If one can and WANTS to help, he can altruistically do so, if not, it's not a problem, nobody is pointing a gun at anybody ! But repeating phrases such as "Read the manual", "Use Google" is just redundant and the only thing you accomplish is increase the number of "no-substance-just-trying-to-be-arrogant" posts.
 
Old 11-11-2010, 05:22 AM   #6
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 292

Rep: Reputation: 46
Quote:
Originally Posted by andy2008 View Post
I tried: grep -w "^john.stewart:" /etc/passwd
Why "-w"?
 
Old 11-11-2010, 05:34 AM   #7
andy2008
Member
 
Registered: Nov 2010
Posts: 40

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by rupertwh View Post
Why "-w"?
Wow, I can't believe that was the problem. You're absolutelly right! It works perfectly without "-w". I was using it because I thought it helped to match the exact string... guess that in my case it was doing the exact opposite... i'm not quite sure why.

Thanks a lot!

Last edited by andy2008; 11-11-2010 at 05:35 AM.
 
Old 11-12-2010, 11:09 PM   #8
lesca
Member
 
Registered: Sep 2010
Posts: 56

Rep: Reputation: 0
Try this:

cat yourfile | cut -f1 -d: | grep '\<word\>'

Note:
1. both single quote and the backslash are necessary
2. it is ':' following -d
 you may change this delimiter to suit future file format.

Explanation:
Your problem is how to cut the right column, because grep only grab the lines which matches your patten.

Hope this may solve your problem

Last edited by lesca; 11-12-2010 at 11:19 PM.
 
Old 11-13-2010, 04:12 AM   #9
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by andy2008
Oh, very helpful! Gee, now why didn't I think of that? Oh, wait, as a matter of fact, that's the first thing I did, but it doesn't seem like it helped, does it? You know, since I'm on this FORUM asking for HELP!

Listen, syg00, some people, like you, don't seem to grasp the meaning of the word FORUM. Let me explain, it's easy: If one can and WANTS to help, he can altruistically do so, if not, it's not a problem, nobody is pointing a gun at anybody ! But repeating phrases such as "Read the manual", "Use Google" is just redundant and the only thing you accomplish is increase the number of "no-substance-just-trying-to-be-arrogant" posts
Now, syg00 may not respond to this, but I will.

Let me focus on this:
Quote:
Oh, wait, as a matter of fact, that's the first thing I did, but it doesn't seem like it helped, does it?
So, you're saying you read the man page. Really? If you did, you would have read this:
Code:
       -w, --word-regexp
              Select only those  lines  containing  matches  that  form  whole
              words.   The  test is that the matching substring must either be
              at the  beginning  of  the  line,  or  preceded  by  a  non-word
              constituent  character.  Similarly, it must be either at the end
              of the line or followed by  a  non-word  constituent  character.
              Word-constituent   characters   are  letters,  digits,  and  the
              underscore.
I emphasize the last line so I can pose this question: did your regex match text that was followed by a non-word constituent character? Say, for instance, a colon? Or did your regex match too much--leaving an 'x' (which is a word-constituent character) and causing the -w test to break?

So, either (1) you didn't read it, or (2) you chose to ignore the last sentence instead of figuring it out or asking for clarification. Stop me if I'm getting any of this wrong...

As you stated, this is a forum, and someone like syg00 who (if you notice) has over 8,000 posts to his credit has undoubtedly seen many messages posted by people that (1) have not read man pages, (2) have not searched Google, and (3) immediately gave up after their first attempt didn't give perfect results.

So before you get aggressive and start throwing mud, make sure that you did, in fact, read the man pages. Because, it sure looks like you didn't.

Most everyone here will be happy to help as long as we get the feeling the person on the other end is being honest and putting forward some effort of their own.

Last edited by Dark_Helmet; 11-13-2010 at 05:03 AM.
 
Old 11-13-2010, 05:29 AM   #10
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
You want to be careful about the '.' character in your match string too, because it has a special meaning (matching any character).
 
Old 11-14-2010, 12:39 AM   #11
andy2008
Member
 
Registered: Nov 2010
Posts: 40

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Dark_Helmet View Post
I emphasize the last line so I can pose this question: did your regex match text that was followed by a non-word constituent character? Say, for instance, a colon? Or did your regex match too much--leaving an 'x' (which is a word-constituent character) and causing the -w test to break?

So, either (1) you didn't read it, or (2) you chose to ignore the last sentence instead of figuring it out or asking for clarification. Stop me if I'm getting any of this wrong...

As you stated, this is a forum, and someone like syg00 who (if you notice) has over 8,000 posts to his credit has undoubtedly seen many messages posted by people that (1) have not read man pages, (2) have not searched Google, and (3) immediately gave up after their first attempt didn't give perfect results.

So before you get aggressive and start throwing mud, make sure that you did, in fact, read the man pages. Because, it sure looks like you didn't.

Most everyone here will be happy to help as long as we get the feeling the person on the other end is being honest and putting forward some effort of their own.
Well, the honest truth is that I read the man page... I don't know why but I didn't completely understand what "-w" was supposed to do and when it was useful(fyi, english isn't my first language). But you are right, I've been a complete jerk and I do apologise to syg00 and to everyone else. I don't usually act like this, but I was in a difficult situation for a number of reasons, and well, like I said, I DID read the manual, it's just somehow I missed that part.
 
Old 11-15-2010, 04:55 PM   #12
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
Quote:
Originally Posted by andy2008 View Post
Hi guys, I'm trying to find exact matches of some users in the /etc/passwd file using "grep -w", but it doesn't always work.
For example, I have the following users:


john.stewart:x:579:579::/home/john.stewart:/bin/bash
andy.stewart:x:580:580::/home/andy.stewart:/bin/bash


So, let's say, I want to search for the user "stewart" (which doesn't exist)

grep -w "stewart" /etc/passwd

I get:

john.stewart:x:579:579::/home/john.stewart:/bin/bash
andy.stewart:x:580:580::/home/andy.stewart:/bin/bash


Is there any way to match the EXACT usernames even though there are other partial matches that include underscores, dots, etc?
Try
grep "^stewart$" /etc/passwd
 
Old 11-15-2010, 11:52 PM   #13
andy2008
Member
 
Registered: Nov 2010
Posts: 40

Original Poster
Rep: Reputation: 1
Of course it doesn't work. The correct search string is:

grep "^stewart:" /etc/passwd

It matches every line that starts with stewart and has a colon after it. It works flawlessly.
 
Old 11-16-2010, 06:48 AM   #14
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
Quote:
Originally Posted by andy2008 View Post
Of course it doesn't work. The correct search string is:

grep "^stewart:" /etc/passwd

It matches every line that starts with stewart and has a colon after it. It works flawlessly.
I skimmed the question and only looked at the part where u wanted to match the user name "stewart". Even if my answer was wrong it did give u the correct guideline which I'd appreciate if i were u.
 
  


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


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
[SOLVED] Match datetime by the minute (not an exact match by the second) [mysql] hattori.hanzo Programming 1 10-21-2010 05:43 PM
how to match the exact pattern using grep utility vinaytp Linux - Newbie 3 05-11-2009 12:36 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
how to find an exact substring match? ldp Programming 7 02-22-2005 06:28 AM


All times are GMT -5. The time now is 08:12 PM.

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