grep multiple keywords, AND not OR

June 21st, 2007 mysurface Posted in egrep, grep, Text Manipulation | Hits: 131683 | 21 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.

21 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 Their product is call They made a custom Extranet for us for our company to use but their main product site is 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

  12. Definitely, what a fantastic site and enlightening posts, I will bookmark your site.Best Regards!

  13. My brother suggested I might like this web site. He was once totally
    right. This put up truly made my day. You cann’t imagine simply how so much time I had spent for this info!

    Feel free to surf to my website – casinognomen

  14. My brother recommended I might like this web site.
    He was totally right. This post truly made
    my day. You can not imagine simply how much time I had spent for this
    information! Thanks!

  15. It’s amazing to pay a quick visit this web page andd reading tthe
    viewws of aall mates regarding this article, while I am also zealous off getting know-how.

  16. He freely admits to experiencing difficult relationships with his mother and father and with his ex-wives.
    Doses are increased as they see that patients are safely
    tolerating the drug until the maximum safe dose is reached.
    Global – Data has found the existing competition in the sepsis therapeutics market to be weak.

  17. Ésta pasa al torrente sanguíneo y es eliminada por el riñón en la orina.

  18. Si crees en este tipo de adivinaciones, por lo menos con Lectura del Tarot te podrás
    ahorrar los euros que puede valer una llamada a una línea de pago.

  19. It has the like you study my mind! You peer to learn a great deal of approximately this specific, just like you had written your guidebook inside it or something.. free cccam server I believe you is capable of doing with many Per-cent to demand the solution household a lttle bit, however in lieu of that will, this can be wonderful blog. An excellent study. I will definitely be back again.

  20. Felicidades por el post. Una información muy interesante.
    Saludos! :)

Leave a Reply