Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to understand the difference between grep -e and grep -E. Now from grep manpage I got:

-E, --extended-regexp

Interpret PATTERN as an extended regular expression (see below).

-e PATTERN, --regexp=PATTERN

Use PATTERN as the pattern; useful to protect patterns beginning with -

The above explanation might be clear to a regex/grep experienced user but for a beginner like me it makes no sense.

So, can someone explain it to me using examples what is the difference between the two and when to use which option.

PS: Version: grep (GNU grep) 2.10

share|improve this question
    
See also: unix.stackexchange.com/questions/17949/… –  Mechanical snail Oct 10 '12 at 19:20
1  
The purpose of -e is really just to disambiguate when a regex starts with a dash. So grep ---foo gives unrecognized option: ---foo but you can say grep -e ---foo to grep for the regular expression ---foo. –  tripleee Oct 10 '12 at 19:31
add comment

migrated from stackoverflow.com Oct 10 '12 at 19:09

This question came from our site for professional and enthusiast programmers.

3 Answers

up vote 7 down vote accepted

-e is strictly the flag for indicating the pattern you want to match against. -E controls whether you need to escape certain special characters.

man grep explains -E it a bit more:

   Basic vs Extended Regular Expressions
   In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).

   Traditional  egrep  did  not  support  the  {  meta-character, and some egrep implementations support \{ instead, so portable scripts should avoid { in grep -E patterns and should use [{] to match a
   literal {.

   GNU grep -E attempts to support traditional usage by assuming that { is not special if it would be the start of an invalid interval specification.  For example, the command grep -E '{1' searches for
   the two-character string {1 instead of reporting a syntax error in the regular expression.  POSIX.2 allows this behavior as an extension, but portable scripts should avoid it.
share|improve this answer
2  
so this is the reason when I do echo "hello" | grep -o -e 'h|l' I get no output because | lost its special meaning and when I do echo "hello" | grep -o -E 'h|l' I get the desired output. –  ronnie Oct 10 '12 at 19:07
1  
Yes. echo "hello" | grep -o -e 'h' -e 'l' will work if you want to toss the -E away in this case. –  hafichuk Oct 10 '12 at 19:11
    
Thanks accepted your answer couldn't upvote b'coz reputation < 15 –  ronnie Oct 10 '12 at 19:16
2  
grep -e -o 'h|l' will match literally h|l while grep -e -o 'h\|l' will match h or l and with -E it's the contrary –  Nahuel Fouilleul Oct 10 '12 at 19:24
    
@ronnie it's unfortunate you need 200+ reputation to have that "feature" follow you between SE sites... –  hafichuk Oct 10 '12 at 19:39
add comment

Also grep -e allows to use several strings for searching: 'grep -e 'abc' -e 'def' -e '123' will look for all three of this strings: abc as well as def and 123.

This works quite similar to grep 'abc\|def\|123' where \| stands for or but could be a bit clearer to read.

As the most important facts on grep -E are already explained here, I just want to add what I summed up on this topic on a quite similar question: Regular Expression for finding double characters in Bash

share|improve this answer
add comment

see below

/extended

grep understands three different versions of regular expression syntax: “basic,” “extended” and “perl.” In GNU grep, there is no difference in available functionality between basic and extended syntaxes. In other implementations, basic regular expressions are less powerful. The following description applies to extended regular expressions; differences for basic regular expressions are summarized afterwards. Perl regular expressions give additional functionality, and are documented in pcresyntax(3) and pcrepattern(3), but may not be available on every system.

So, once again.

In GNU grep, there is no difference in available functionality between basic and extended syntaxes

share|improve this answer
1  
the functionality is the same between basic and extended but the syntax is slightly different. regexp special chars like (, ), | etc need to be backslash-escaped to have their special meaning in basic regexp, but not in extended (where they need to be escaped to be treated as a string literal) –  cas Oct 11 '12 at 5:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.