Monday, September 22, 2014

Grep with Multiple Expressions

Occasionally, you want to find all the lines in a file which have either 'eng', or 'net', or 'ide' in them.

You think "I know, I'll use regular expressions".

Now, you have three problems. The one you weren't expecting is grep's weird syntax for regular expressions.

This subject is both deep and boring. Suffice it to say that:

grep 'eng\|net\|ide'

is an answer

grep -E 'eng|net|ide'

and

ack 'eng|net|ide'

is an answer.

If you really want to know the details, look up 'basic regular expressions', 'extended regular expressions', and 'perl-compatible regular expressions'.



2 comments:

  1. Regular expressions are overkill for that purpose, since you can just use the -e parameter:

    grep -e eng -e net -e ide

    Also useful is the -f parameter if the patterns happen to be in a file

    ReplyDelete
  2. Regular expressions are overkill for that purpose, since you can just use the -e parameter:

    grep -e eng -e net -e ide

    Also useful is the -f parameter if the patterns happen to be in a file

    ReplyDelete