Bird
Raised Fist0
Linux CLIscripting~10 mins

grep with regex (-E) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - grep with regex (-E)
Start: Input file/text
↓
Run grep -E with regex
↓
grep reads each line
↓
Check if line matches regex
↓
Print line
↓
Next line
↓
End of file?
↓
Repeat
grep with -E reads each line of input and prints only those lines that match the extended regular expression.
Execution Sample
Linux CLI
grep -E 'cat|dog' animals.txt
Searches animals.txt and prints lines containing 'cat' or 'dog'.
Execution Table
StepLine ContentRegex Match?ActionOutput
1I have a catYesPrint lineI have a cat
2birds are flyingNoSkip lineI have a cat
3My dog barksYesPrint lineI have a cat My dog barks
4Fish swimNoSkip lineI have a cat My dog barks
5Cats and dogsYesPrint lineI have a cat My dog barks Cats and dogs
6End of fileNoStopI have a cat My dog barks Cats and dogs
💡 Reached end of file, grep stops reading lines.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Current LineI have a catbirds are flyingMy dog barksFish swimCats and dogsEnd of file
Match FoundYesNoYesNoYesNo
Output LinesI have a catI have a catI have a cat My dog barksI have a cat My dog barksI have a cat My dog barks Cats and dogsI have a cat My dog barks Cats and dogs
Key Moments - 3 Insights
Why does grep print some lines but not others?
grep prints only lines where the regex matches (see execution_table rows 1,3,5). Lines without a match are skipped (rows 2,4,6).
What does the -E option change in grep?
-E enables extended regex, allowing use of | for OR without escaping. This is why 'cat|dog' matches lines with either word.
Why is the output empty for some steps in the table?
When a line does not match, grep skips printing it, so output is empty for those steps (rows 2,4,6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 3?
A"I have a cat"
B"My dog barks"
C"I have a cat\nMy dog barks"
D"Cats and dogs"
💡 Hint
Check the Output column after step 3 in the execution_table.
At which step does grep first skip printing a line?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for the first 'No' in Regex Match? column and empty Output in execution_table.
If the regex was changed to 'cat|bird', which step's output would change?
AStep 4
BStep 2
CStep 5
DStep 6
💡 Hint
Check which lines contain 'bird' and see their match status in execution_table.
Concept Snapshot
grep -E 'pattern' file
- Uses extended regex for matching
- Reads file line by line
- Prints lines matching regex
- Supports | for OR without escapes
- Stops at end of file
Full Transcript
This visual trace shows how grep with the -E option works. It reads each line of the input file and checks if the line matches the extended regular expression. If it matches, grep prints the line; if not, it skips it. The process repeats until the end of the file is reached. The example uses the regex 'cat|dog' to find lines containing either 'cat' or 'dog'. The execution table tracks each line, whether it matches, and the output produced. The variable tracker shows how the current line, match status, and output lines change step by step. Key moments clarify why some lines print and others don't, and how -E enables easier regex syntax. The quiz tests understanding of output at different steps and effects of changing the regex.

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

  1. Step 1: Understand grep options

    The -E option tells grep to use extended regular expressions, which allow special symbols like |, +, and ().
  2. Step 2: Compare with other options

    Other options like -i ignore case, and -n print line numbers, but -E specifically enables extended regex.
  3. Final Answer:

    Enables extended regular expressions for more complex pattern matching -> Option D
  4. 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

  1. Step 1: Understand the regex for alternatives

    The symbol | means OR in extended regex, so cat|dog matches lines with either "cat" or "dog".
  2. 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.
  3. Final Answer:

    grep -E 'cat|dog' filename -> Option A
  4. 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

  1. Step 1: Analyze the regex pattern

    The pattern ^apple$|banana$ means lines exactly matching "apple" OR lines ending with "banana".
  2. 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".
  3. Final Answer:

    apple banana -> Option C
  4. 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

  1. Step 1: Check regex syntax and grep version

    Some grep versions require escaping parentheses even with -E, so unescaped () cause invalid regex error.
  2. Step 2: Verify other options

    Quoting is done, + is valid in extended regex, and file existence error would be different.
  3. Final Answer:

    The parentheses are not escaped and your grep version does not support unescaped () -> Option B
  4. 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

  1. Step 1: Understand character classes and alternation

    Character class [-/] matches either '-' or '/'. Using [0-9] matches digits. Curly braces specify exact counts.
  2. 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.
  3. Final Answer:

    grep -E '[0-9]{4}[-/][0-9]{2}[-/][0-9]{2}' log.txt -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard