0
0
Linux CLIscripting~10 mins

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

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