grep multiple keywords, AND not OR
June 21st, 2007 mysurface Posted in Text Manipulation, egrep, grep | Hits: 27407 |
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.
Live Chat!









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
[...] http://linux.byexamples.com/archives/290/grep-multiple-keywords-and-not-or/ [...]