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/)

astrogeek 11-02-2017 03:27 PM

Welcome to LQ vaibhav5587!

You have replied to a thread that has been quiet for seven years.

If you have a similar problem or simply wish to share your own knowledge, please consider starting your own thread to get better exposure and attract discussion from currently active members.

MadeInGermany 11-02-2017 05:25 PM

If you have users like "andy.stewart" where the dot is a wildcard character in RE then I would use a string match in awk.
Code:

awk -F: '$1=="andy.stewart" {print; m=1} END {exit !m}'
An interesting variant is to quit after the first match
Code:

awk -F: '$1=="andy.stewart" {print; m=1; exit} END {exit !m}'
Note that an exit in the main loop jumps to the END section.

David the H. 11-05-2017 02:03 AM

I'd just like to focus for a second on the conceptual background at work here.

Regular expressions are designed for matching, amazingly enough, text strings that follow regular patterns. So to properly create a regex rule, you first have to step back and define for yourself exactly what it is that you need to match; to find a pattern that makes the string you want unique from all other similar ones you may encounter. And preferably the simplest one, if multiple possibilities are available.

Remember that regex processes lines from left to right, so your goal is often as much or more about properly matching the starting and ending points of the string as the content itself. Does "string" always start with the same letter? Is it always preceded and/or followed by the same character or pattern? Is it always found in the same location in the line, or is always the same length? Is some character always found in the string, or never found in it? Does "I" always come before "E", except after "C"? Things like that.

If you cannot clearly define the string in a regular way like this, you can't use regex on it, naturally.

So in the OP, you want to match the username in /etc/passwd. Since that file is well-defined and regularly delimited, and the username is always the first entry on the line, it turns out that the string that works best is simply "line start" + "username" + "colon". Now that we have properly defined the problem, the regex itself is easy enough to construct. All too often users over-complicate things by trying to match something much more complex than is really needed.

Of course the opposite often happens too; you create a simple regex that appears to work just fine, only to find that the input is more variable than initially expected and it fails to always match as planned. Again, properly defining the matching pattern first helps to avoid such problems.

It also goes without saying that you have to understand how regex works, as well as the tool you are using to apply it with. That just takes study and experience.


And finally, with all this said, I would actually recommend awk in this case instead, such as that posted by MadeInGermany just above. awk is designed specifically for working on regularly-delimited text strings like this. The right tool for the right job and all that jazz.

ericnyamu 02-14-2019 07:56 AM

Quote:

Originally Posted by lesca (Post 4157369)
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

thanks it worked. pretty neat

TB0ne 02-14-2019 08:36 AM

::removed::


All times are GMT -5. The time now is 04:40 AM.