LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using grep to find EXACT MATCH (https://www.linuxquestions.org/questions/programming-9/using-grep-to-find-exact-match-843612/)

andy2008 11-11-2010 02:40 AM

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?

syg00 11-11-2010 02:52 AM

Actually I've yet to see a case where it doesn't work - as defined.
Proper specification of the regex usually works just fine.

andy2008 11-11-2010 04:37 AM

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?

syg00 11-11-2010 04:49 AM

Quote:

Originally Posted by andy2008 (Post 4155604)
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".

andy2008 11-11-2010 05:12 AM

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.

rupertwh 11-11-2010 05:22 AM

Quote:

Originally Posted by andy2008 (Post 4155604)
I tried: grep -w "^john.stewart:" /etc/passwd

Why "-w"?

andy2008 11-11-2010 05:34 AM

Quote:

Originally Posted by rupertwh (Post 4155632)
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!

lesca 11-12-2010 11:09 PM

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

Dark_Helmet 11-13-2010 04:12 AM

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.

neonsignal 11-13-2010 05:29 AM

You want to be careful about the '.' character in your match string too, because it has a special meaning (matching any character).

andy2008 11-14-2010 12:39 AM

Quote:

Originally Posted by Dark_Helmet (Post 4157482)
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.

ghantauke 11-15-2010 04:55 PM

Quote:

Originally Posted by andy2008 (Post 4155528)
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

andy2008 11-15-2010 11:52 PM

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.

ghantauke 11-16-2010 06:48 AM

Quote:

Originally Posted by andy2008 (Post 4160255)
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.

vaibhav5587 11-02-2017 05:57 AM

Try this
 
try this:
grep -P '^(tomcat!?)' tst1.txt

It will search for specific/exact word in txt file. Here we are trying to search word 'tomcat'


All times are GMT -5. The time now is 10:34 AM.