0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 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.