The -E option tells grep to use extended regular expressions (ERE), allowing more powerful pattern matching without needing to escape some special characters.
Click to reveal answer
beginner
How is grep -E different from basic grep?
Basic grep uses basic regular expressions (BRE) where some characters like ?, +, and {} need escaping. grep -E uses extended regex where these characters work directly.
Click to reveal answer
beginner
Example: What does grep -E 'cat|dog' file.txt do?
It searches file.txt for lines containing either 'cat' or 'dog'. The | means OR in extended regex.
Click to reveal answer
intermediate
Why use grep -E instead of egrep?
egrep is an older command equivalent to grep -E. Modern systems recommend using grep -E because egrep may be deprecated.
Click to reveal answer
beginner
How to match one or more digits using grep -E?
Use grep -E '[0-9]+'. The + means 'one or more' of the previous character or group.
Click to reveal answer
What does the command grep -E 'a+b' file.txt match?
ALines with 'a+b' literally
BLines with 'a' or 'b'
CLines with 'ab' only
DLines with one or more 'a' followed by 'b'
✗ Incorrect
The + means one or more 'a's before 'b' in extended regex.
Which symbol means OR in grep -E regex?
A*
B|
C?
D.
✗ Incorrect
The pipe | means OR in extended regular expressions.
Which command is recommended for extended regex searching?
Agrep -E
Bgrep -P
Cgrep -B
Degrep
✗ Incorrect
grep -E is the modern recommended way to use extended regex.
How to match zero or one occurrence of 'a' using grep -E?
Aa?
Ba*
Ca+
Da|
✗ Incorrect
The ? means zero or one occurrence in extended regex.
Explain how to use grep -E to search for lines containing either 'apple' or 'orange' in a file.
Think about how to combine two words with OR in regex.
You got /4 concepts.
Describe the difference between basic and extended regular expressions in the context of grep.
Focus on special characters and how they behave.
You got /4 concepts.
Practice
(1/5)
1. What does the -E option do when used with the grep command?
easy
A. Ignores case when searching
B. Searches only for exact string matches without regex
C. Prints line numbers along with matching lines
D. Enables extended regular expressions for more complex pattern matching
Solution
Step 1: Understand grep options
The -E option tells grep to use extended regular expressions, which allow special symbols like |, +, and ().
Step 2: Compare with other options
Other options like -i ignore case, and -n print line numbers, but -E specifically enables extended regex.
Final Answer:
Enables extended regular expressions for more complex pattern matching -> Option D
Quick Check:
grep -E = extended regex [OK]
Hint: Remember: -E means extended regex for flexible patterns [OK]
Common Mistakes:
Confusing -E with -i (ignore case)
Thinking -E disables regex
Mixing -E with line number option -n
2. Which of the following is the correct syntax to search for lines containing either "cat" or "dog" using grep -E?
easy
A. grep -E 'cat|dog' filename
B. grep -E 'cat+dog' filename
C. grep -E 'cat?dog' filename
D. grep -E 'cat.dog' filename
Solution
Step 1: Understand the regex for alternatives
The symbol | means OR in extended regex, so cat|dog matches lines with either "cat" or "dog".
Step 2: Check other regex symbols
+ means one or more of previous character, ? means zero or one, and . means any single character, so they don't match the OR condition.
Final Answer:
grep -E 'cat|dog' filename -> Option A
Quick Check:
Use | for OR in grep -E [OK]
Hint: Use | inside quotes for OR patterns with grep -E [OK]
Common Mistakes:
Using + or ? instead of | for OR
Not quoting the pattern
Confusing . with |
3. Given a file data.txt with these lines:
apple
banana
applepie
pineapple
banana split
What is the output of grep -E '^apple$|banana$' data.txt?
medium
A. applepie
banana
B. apple
banana split
C. apple
banana
D. apple
banana
banana split
Solution
Step 1: Analyze the regex pattern
The pattern ^apple$|banana$ means lines exactly matching "apple" OR lines ending with "banana".
Step 2: Match lines in the file
"apple" matches ^apple$. "banana" matches banana$. "applepie" does not match ^apple$ due to extra characters after "apple". "pineapple" matches neither. "banana split" does not end with "banana".
Final Answer:
apple
banana -> Option C
Quick Check:
^apple$ or banana$ matches apple and banana [OK]
Hint: Remember ^ means start, $ means end in regex [OK]
Common Mistakes:
Assuming banana split matches banana$
Matching applepie because it contains apple
Ignoring anchors ^ and $
4. You run grep -E '(foo|bar)+' file.txt but get an error: grep: invalid regular expression. What is the likely cause?
medium
A. You forgot to quote the pattern
B. The parentheses are not escaped and your grep version does not support unescaped ()
C. The + symbol is not valid in extended regex
D. The file.txt does not exist
Solution
Step 1: Check regex syntax and grep version
Some grep versions require escaping parentheses even with -E, so unescaped () cause invalid regex error.
Step 2: Verify other options
Quoting is done, + is valid in extended regex, and file existence error would be different.
Final Answer:
The parentheses are not escaped and your grep version does not support unescaped () -> Option B
Quick Check:
Escape () if grep -E errors [OK]
Hint: Escape parentheses if grep -E gives regex error [OK]
Common Mistakes:
Assuming + is invalid in extended regex
Not quoting pattern causing shell errors
Confusing file not found with regex error
5. You want to find lines in log.txt that contain a date in format YYYY-MM-DD or YYYY/MM/DD using grep -E. Which pattern correctly matches both formats?
hard
A. grep -E '[0-9]{4}[-/][0-9]{2}[-/][0-9]{2}' log.txt
B. grep -E '[0-9]{4}-|/[0-9]{2}-|/[0-9]{2}' log.txt
C. grep -E '\d{4}[-/]\d{2}[-/]\d{2}' log.txt
D. grep -E '[0-9]{4}[-|/][0-9]{2}[-|/][0-9]{2}' log.txt
Solution
Step 1: Understand character classes and alternation
Character class [-/] matches either '-' or '/'. Using [0-9] matches digits. Curly braces specify exact counts.
Step 2: Analyze each option
grep -E '\d{4}[-/]\d{2}[-/]\d{2}' log.txt uses \d which is not supported by grep regex. grep -E '[0-9]{4}-|/[0-9]{2}-|/[0-9]{2}' log.txt uses ungrouped -|/ causing incorrect alternation that splits the pattern. grep -E '[0-9]{4}[-|/][0-9]{2}[-|/][0-9]{2}' log.txt uses [-|/] which includes '|' as a character, incorrect. grep -E '[0-9]{4}[-/][0-9]{2}[-/][0-9]{2}' log.txt correctly uses character classes [-/] without extra characters.
Final Answer:
grep -E '[0-9]{4}[-/][0-9]{2}[-/][0-9]{2}' log.txt -> Option A
Quick Check:
Use [0-9]{n} and [-/] for digits and separators [OK]
Hint: Use [0-9] and [-/] for digits and dash or slash [OK]
Common Mistakes:
Using \d which grep does not support
Including | inside character class incorrectly
Using parentheses without escaping in some grep versions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.