grep multiple keywords, AND not OR
June 21st, 2007 mysurface Posted in egrep, grep, Text Manipulation | Hits: 130213 | 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.
June 21st, 2007 at 5:28 am
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?
June 21st, 2007 at 4:25 pm
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?
June 21st, 2007 at 11:04 pm
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 ;)
June 22nd, 2007 at 11:37 am
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
July 22nd, 2007 at 8:47 am
[…] grep multiple keywords, AND not OR (tags: and examples command Search sysadmin linux cli grep) […]
August 10th, 2007 at 1:30 am
[…] 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… […]
August 10th, 2007 at 6:39 am
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.
September 5th, 2008 at 3:45 am
[…] https://linux.byexamples.com/archives/290/grep-multiple-keywords-and-not-or/ […]
June 25th, 2009 at 10:23 pm
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”
August 9th, 2012 at 12:02 am
Your improved command will not find “me and you”.
April 10th, 2013 at 11:42 am
It is best to take part in a contest for among the best blogs on the web. I’ll advocate this web site!
May 6th, 2013 at 9:22 pm
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
April 24th, 2014 at 12:00 am
Definitely, what a fantastic site and enlightening posts, I will bookmark your site.Best Regards!
July 3rd, 2014 at 4:27 am
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!
Thanks!
Feel free to surf to my website – casinognomen
September 17th, 2014 at 10:42 pm
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!
October 9th, 2014 at 9:03 am
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.
May 5th, 2015 at 3:19 pm
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.
August 27th, 2015 at 3:24 am
Ésta pasa al torrente sanguíneo y es eliminada por el riñón en la orina.
September 18th, 2015 at 10:29 pm
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.
March 14th, 2017 at 6:03 am
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.
December 20th, 2017 at 8:48 am
Felicidades por el post. Una información muy interesante.
Saludos! :)