7 Grep Command Examples With Extended Regex in LinuxUnix
7 Grep Command Examples With Extended Regex in LinuxUnix
7 Grep Command Examples With Extended Regex in LinuxUnix
Linux/Unix
linuxnix.com /grep-command-regular-expressions-examples-iii/
In our the previous post we saw how to use Basic regular expression along with grepping to search for words and
played across different basic regular expressions. In this post, we will see how to use extended regular
expressions to increase the power of grep command even better than Basic regular expression.
Note1: To use this extended regular expressions we have to use E option give grep the capability to understand
Extended regular expressions.
1/3
Note2: egrep command is nothing but grep with E option, so please dont learn it. If the grep command it self can
do the work for you, why to learn new Linux command?
Examples:
Example1: Search for a word which contains one or more occurrence of b between a and c. The below command
will search for words like abc, abbc, abbbc etc.
grep E ab+c
filename
Example2: Search for a word which contains zero or one occurrence of b between a and c. The below command
will search for ac, abc. This is a bit tricky as the first search ac do not contain b. As ? states, it searches for zero or
one occurrence of a character.
grep E ab?c
filename
Suppose you want to search for john and you want to search for marry as well. Do not use two grep commands as
shown.
grep marry
filename
Instead of using two grep commands, use -E options to search multiple words with single grep command.
grep -E 'john|marry|tony'
filename
Example4: Search for a word which contains either a or b, but not both a and b between d, e characters
2/3
grep E ab{2}c
filename
Example6: Search for a word which contains 3 to 4 b between a and c character. so this will search for abbc,
abbbc, and abbbbc as well.
grep E ab{2,4}c
filename
Example7: Search for a word which contains 3 or more b between a and c character. This will search for abbbc,
abbbbc, and so on.
Note: When we are using {} we have to give a range, but in this example, we did not provide range we just started
the range but did not end the range which indicates infinity.
3/3