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.
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.