mercoledì 4 aprile 2012

Grep su Stringhe multiple (linux)

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"
not good enough

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 "\.*\" 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.