Use the -E or -e flag to treat your search term as a regular expression. The lowercase -e is used for basic regular expressions which includes: ^$.[]*.
The uppercase -E includes the extended regular expressions: (){}?~|.
Let’s go back to our file test.txt with the following content:
sad
happy
awake
coffee
work
schoolSuppose we wanted to use a regular expression that searches for all the words that ends in “e”. We would do something like this:
$ grep -e "e$" test.txt
awake
coffeeIn reality, this already happens by default. You could easily run the same grep command without the -e flag and get the same results. The real power is being able to use -e multiple times to construct the perfect search. Let’s say we wanted to search for all words that end in “e” or starts with “w”.
We could accomplish that like this:
$ grep -e "e$" -e "^w" test.txt
awake
coffee
workIf you wanted to use the extended regular expressions like the | symbol, you must use -E.
For example this is the search for lines with “sad” or “happy”:
$ grep -E "sad|happy" test.txt
sad
happyUsing -e with the above would return zero rows.
Want to see this in action? Check out the video below.