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.

Extended regular expressions:

+ --Match one or more occurrences of the previous character.

| -- Match Either character

? – Match 0 or 1 occurrence of the previous character.

() –match a group of characters

{number} –Match number of occurrence of a character

{1, 3} –Match a character which is 1 to 3 times repetition

{5, } –Match a repeated character which is repeated 5 or more times.

Note1: To use this extended regular expressions we have to use –E option give grep the capability to understand Extended regular expressions.

Note2: egrep command is nothing but grep with –E option, so please don’t learn it. If the grep command it self can do the work for you, why to learn new Linux command?

Related concept:   BASH_History_Capabalities

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

Example3: Search for multiple words with single grep command.

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 john filename

grep marry filename

Instead of using two grep commands, use -E options to search multiple words with single grep command.

grep –E ‘john|marry’ filename 

How about searching three 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

grep –E ‘d(a|b)e’ filename 

Interval regular expressions with grep

Example5: Search for a word which contains only 2 ‘b’ between a and c character

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.

grep –E ‘ab{3, }c’ filename 

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.

Related concept:   How To Assign Default Gateway In Linux?

Please share your thoughts on this.