Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Extended regex (grep -E) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Extended regex (grep -E)
Start: Input text file
↓
Run grep -E with pattern
↓
grep -E interprets extended regex
↓
Search text lines for matches
↓
Print matching lines
↓
End
The flow shows how grep -E reads a file, uses extended regex to find matches, and prints matching lines.
Execution Sample
Bash Scripting
echo -e "cat\ndog\nbat\nball" | grep -E "ba(l|t)"
This command filters lines containing 'bal' or 'bat' using extended regex.
Execution Table
StepInput LinePattern TestedMatch ResultOutput Line
1catba(l|t)No
2dogba(l|t)No
3batba(l|t)Yesbat
4ballba(l|t)Yesball
5End of input---
💡 All lines processed; grep -E prints lines matching extended regex pattern.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Input LinecatdogbatballEndEnd
Match FoundNoNoYesYes--
Output Linesbatbat ballbat ballbat ball
Key Moments - 3 Insights
Why does 'cat' not match the pattern 'ba(l|t)'?
'cat' does not start with 'ba', so it fails the pattern 'ba(l|t)'. See execution_table row 1 where Match Result is 'No'.
What does the '(l|t)' part mean in the pattern?
It means either 'l' or 't' can appear after 'ba'. This is why 'bat' and 'ball' match, as shown in rows 3 and 4.
Why do we use grep -E instead of plain grep here?
grep -E enables extended regex syntax like alternation (|) without backslashes, making patterns easier to write and read.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which input line first matches the pattern?
Abat
Bcat
Cdog
Dball
💡 Hint
Check the 'Match Result' column in execution_table rows 1-4.
At which step does grep -E stop processing input lines?
AAfter step 3
BAfter step 4
CAfter step 5
DIt never stops
💡 Hint
Look at the 'Step' and 'Input Line' columns; step 5 marks end of input.
If the pattern changed to 'ba(l|t|g)', which new line would match?
Acat
Bbag
Cbat
Dball
💡 Hint
Check which lines start with 'ba' followed by 'l', 't', or 'g'.
Concept Snapshot
grep -E 'pattern' file
- Uses extended regex for matching
- Supports alternation (|), grouping () without escapes
- Prints lines matching pattern
- Useful for complex text searches
- Example: grep -E 'ba(l|t)' matches 'bal' or 'bat'
Full Transcript
This visual execution traces the command 'echo -e "cat\ndog\nbat\nball" | grep -E "ba(l|t)"'. The input lines are tested one by one against the extended regex pattern 'ba(l|t)'. Lines 'bat' and 'ball' match because they start with 'ba' followed by either 'l' or 't'. Lines 'cat' and 'dog' do not match. grep -E prints only the matching lines. The process ends after all lines are checked. Key points include understanding the alternation operator (|) and why grep -E is needed for extended regex syntax.

Practice

(1/5)
1. What does the grep -E command do in bash scripting?
easy
A. Runs grep with extra debugging information
B. Enables extended regular expressions for more powerful pattern matching
C. Executes grep in encrypted mode
D. Searches only for exact string matches without regex

Solution

  1. Step 1: Understand grep basic function

    grep searches text for patterns. By default, it uses basic regex.
  2. Step 2: Role of -E option

    The -E option enables extended regex, allowing special characters like +, ?, | without backslashes.
  3. Final Answer:

    Enables extended regular expressions for more powerful pattern matching -> Option B
  4. Quick Check:

    grep -E = extended regex enabled [OK]
Hint: Remember: -E means extended regex, more pattern power [OK]
Common Mistakes:
  • Confusing -E with debugging or encryption
  • Thinking -E disables regex
  • Assuming -E is for exact string match only
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 OR operator in extended regex

    The vertical bar | means OR, matching either pattern on left or right.
  2. Step 2: Check each option's pattern

    'cat|dog' matches lines with 'cat' or 'dog'. Others (&, ?, +) do not mean OR.
  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 & instead of | for OR
  • Confusing ? or + as OR
  • Not quoting the pattern
3. Given a file data.txt with the following lines:
apple
apples
applepie
appl
apply

What is the output of grep -E 'apple(s|pie)?' data.txt?
medium
A. applepie
B. apple apples applepie appl apply
C. apples applepie
D. apple apples applepie

Solution

  1. Step 1: Understand the regex pattern 'apple(s|pie)?'

    This matches 'apple' followed optionally by either 's' or 'pie'. So it matches 'apple', 'apples', or 'applepie'.
  2. Step 2: Check each line against the pattern

    'apple' matches exactly.
    'apples' matches 'apple' + 's'.
    'applepie' matches 'apple' + 'pie'.
    'appl' and 'apply' do not match because they lack the full 'apple' base.
  3. Final Answer:

    apple apples applepie -> Option D
  4. Quick Check:

    Optional group (s|pie)? matches these three lines [OK]
Hint: Remember ? means optional, (a|b) means either a or b [OK]
Common Mistakes:
  • Assuming partial matches like 'appl' count
  • Ignoring the optional group ?
  • Confusing | with + or ?
4. You run the command grep -E colou?r file.txt but it returns no matches, even though the file contains the word "color". What is the likely problem?
medium
A. The pattern is missing the optional character syntax
B. The file does not contain the word "color"
C. The regex should be quoted to prevent shell expansion
D. The pattern is correct but the file encoding is wrong

Solution

  1. Step 1: Check the pattern and shell quoting

    The pattern 'colou?r' uses ? to make 'u' optional, matching 'color' or 'colour'.
  2. Step 2: Identify quoting issue

    If the pattern is not quoted, the shell may interpret ?, causing grep to receive a wrong pattern and no matches.
  3. Final Answer:

    The regex should be quoted to prevent shell expansion -> Option C
  4. Quick Check:

    Always quote regex patterns to avoid shell issues [OK]
Hint: Always quote regex patterns to avoid shell interpreting special chars [OK]
Common Mistakes:
  • Not quoting regex patterns
  • Assuming file lacks the word
  • Blaming file encoding without checking
5. You want to find lines in log.txt that contain either "error", "fail", or "warning" using grep -E. Which command correctly matches these words as whole words only?
hard
A. grep -E '\b(error|fail|warning)\b' log.txt
B. grep -E '(error|fail|warning)' log.txt
C. grep -E '\<(error|fail|warning)\>' log.txt
D. grep -E '[error|fail|warning]' log.txt

Solution

  1. Step 1: Understand word boundaries in extended regex

    \b marks word boundaries, ensuring the pattern matches whole words only.
  2. Step 2: Analyze each option

    grep -E '\b(error|fail|warning)\b' log.txt uses \b around the group, correctly matching whole words.
    grep -E '(error|fail|warning)' log.txt matches substrings inside words.
    grep -E '\<(error|fail|warning)\>' log.txt uses \< and \> which are not standard in grep -E.
    grep -E '[error|fail|warning]' log.txt uses character class [] incorrectly, matching any single character from the set.
  3. Final Answer:

    grep -E '\b(error|fail|warning)\b' log.txt -> Option A
  4. Quick Check:

    Use \b for whole word matching in grep -E [OK]
Hint: Use \b to match whole words in grep -E patterns [OK]
Common Mistakes:
  • Using character classes [] instead of grouping ()
  • Not using word boundaries \b
  • Using non-standard boundary markers like \< or \>
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