Tuesday, September 09, 2008

grep based on multiple words

For example, grep "you" and "me" in the file,

grep "you\|me" filename

You need to put ESCAPE STRING ( \ ) for OR ( | ), else it will treat it as a simbol you want to search instead of regular expression symbol. or

grep -E "you|me" filename


egrep "you|me" filename

But, to get line with both "you" and "me", you can use

grep "you" filename | grep "me"


egrep "you.*me" filename

but this will include those lines like "your lovely meebo", which is not what we want sometime. So, to get the exact words matched, use

egrep "\.*\" filename

For more info about egrep, use "man egrep"

The caret ^ and the dollar sign $ are metacharacters that respectively match the  empty  string  at  the beginning and end of a line.  The symbols \<> respectively match the empty string at the beginning  and end of a word.  The symbol \b matches the empty string at the edge of a word,  and  \B  matches  the  empty string provided it not at the edge of a word.

No comments:

Post a Comment