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? |
Actually I've yet to see a case where it doesn't work - as defined.
Proper specification of the regex usually works just fine. |
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? |
Quote:
regex is a slippery subject - as I am wont to say "regex ain't regex". |
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. |
Quote:
|
Quote:
Thanks a lot! |
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 |
Quote:
Let me focus on this: Quote:
Code:
-w, --word-regexp 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. |
You want to be careful about the '.' character in your match string too, because it has a special meaning (matching any character).
|
Quote:
|
Quote:
grep "^stewart$" /etc/passwd |
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. |
Quote:
|
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. |