Operations with grep in Linux
Table of Contents
Basic vs extended regex in grep#
With basic regex expressions (short BRE), special characters must be preceeded with a backslash if they are to be interpreted as special. Otherwise, they are interpreted as regular characters. The exception to this rule is the carrot character (^). BRE mode is activated with the -e option.
Let us consider a sample file /tmp/test.log":
Wassim@linux:/tmp$ cat test.log
a+b
a plus b
aaaaaaaab
Wassim@linux:/tmp$
Example 1: grep -e and passing the special characters without a backslash:
Wassim@linux:/tmp$ grep -e 'a+b' test.log
a+b
Wassim@linux:/tmp$
Example 2: grep -e and passing special characters wit a backslash:
Wassim@linux:/tmp$ grep -e 'a\+b' test.log
aaaaaaaab
Wassim@linux:/tmp$
grep -e 'a\+b' test.log searched for patterns like ab, aab, aaaab, etc. in the /tmp/test.log file.
With extended regex expressions (short ERE), special characters are interpreted as special without the need for a backslash.
Example 3: grep -E and passing special characters. No backslash involved.
Wassim@linux:/tmp$ grep -E 'a+b' test.log
aaaaaaaab
Wassim@linux:/tmp$