grep multiple keywords, AND not OR

June 21st, 2007 mysurface Posted in egrep, grep, Text Manipulation | Hits: 99639 | 12 Comments »

This morning, I asked to analyze log files where I need to grep the lines with two keywords, and the post grep based on multiple keywords doesn’t help much.

Refering to the post, where it grep the lines either keyword1 OR keyword2 appears. Where the case I intended to get is different, I need to grep the lines with keyword1 AND keyword2. Look at the sample raw data for example.

Filename: world.txt


you and marry
rose and me
you and me

Assume my keyword1 is you, keyword2 is me, what I intended to get is the only last line , which is you and me.


egrep "you|me" world.txt

The command line above returns me all three lines, that is not what I want.

The solution I can think of is grep two times, which passes through pipeline.


grep "you" world.txt | grep "me"

After I discuss with geek00l, he shows me the prominent regex.


egrep "you.*me" world.txt

.* indicate match any characters in between “you” and “me”. He later further improve his regex and the outcome is this:


egrep "\<you\>.*\<me\>" world.txt

The problem of this regex “you.*me” will match something like “your lovely meow”, which it is not the expected outcome. By encapsulated with \< and \>, it will match only with exact keyword.

12 Responses to “grep multiple keywords, AND not OR”

  1. Brad Aldridge Says:

    My company needs wireless data collection tool that will enable me to make custom forms on my Blackberry or Motorola phone. The form need to support drop down menus, GPS, check boxes, bar coding and photo capture. Can someone help me with this please?

  2. I tried man 7 regex (regex – POSIX.2 regular expressions) but couldn’t find the “\>” description.

    But through google, i found its description at regexp (5), which is not exist in my ubuntu feisty.

    What’s the difference between regex and regxp, and how do we detect which one is supported in our linux distro?

  3. Actually regex includes both BRE(basic regular expression) and ERE(extended regular expression) but regexp is just about basic regular expression.

    You don’t detect which one you use by distro. It is all down to the programming language or tools you are using supports BRE or ERE, and some programming language supports its own set of regex that it thinks more efficient than the others and those are pretty relatives. One clear example you can see is using grep it only supports BRE, but using egrep it supports ERE, and you can turn on perl regex supports with grep -P.

    Hopefully you get what I mean, cheers ;)

  4. Sean Johnson Says:

    I have been working in marketing industry for about 5 years and we found a solution that does this. We use it for wireless bar code reading and GPS tracking. The best part is we can customize the forms on-the-fly which saves us a lot of time over a customized solution. The company that makes the product is Westlake out of LA I think. There website is http://www.westlakesoftware.com. Their product is call AirMobility.net. They made a custom Extranet for us for our company to use but their main product site is http://www.airmobility.net. We found this service set up to make it pretty easy for us to migrate the information to our back office system. We use the technology and route it to 40 different servers nationally; has worked flawlessly for about three years. I am note sure if this will match your exact needs but it is what we have been using and it has been working pretty well for us.
    Sean Johnson, VP-IT

  5. [...] grep multiple keywords, AND not OR (tags: and examples command Search sysadmin linux cli grep) [...]

  6. [...] It is useful to grep logs with multiple lines for one entry. Related Posts grep multiple keywords, AND not OR This morning, I asked to analyze log files where I need to grep the lines with two keywords, and the post grep based on… [...]

  7. Garry Parker Says:

    To match whole words try using

    egrep -w “you.*me” world.txt

    instead of

    egrep “\.*\” world.txt

    It’s easier to type and gives the same results. The man page says the -w matches whole words in egrep.

  8. I am looking for a solution to find words in the complete file – not just one line.
    Is there an easier|faster solution than this one:

    grep -il “you” * | xargs grep -il “me”

  9. Your improved command will not find “me and you”.

  10. It is best to take part in a contest for among the best blogs on the web. I’ll advocate this web site!

  11. After looking over a few of the blog articles on your
    website, I honestly appreciate your way of blogging.
    I saved as a favorite it to my bookmark website list and will be checking back soon.
    Take a look at my website too and tell me what
    you think.

    My weblog; cardsharing servers cccam

Leave a Reply