0
0
Bash Scriptingscripting~10 mins

Basic regex in grep in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Basic regex in grep
Start: Input text file
Run grep with regex
grep reads each line
Check if line matches regex
Print line
End: Output matched lines
grep reads each line of a file, checks if it matches the regex, and prints only matching lines.
Execution Sample
Bash Scripting
grep '^a.*e$' sample.txt
This command prints lines from sample.txt that start with 'a' and end with 'e'.
Execution Table
StepLine ContentRegex Check '^a.*e$'Match ResultAction
1appleStarts with 'a' and ends with 'e'YesPrint line
2bananaDoes not start with 'a'NoSkip line
3aceStarts with 'a' and ends with 'e'YesPrint line
4angleStarts with 'a' and ends with 'e'YesPrint line
5grapeDoes not start with 'a'NoSkip line
6aleStarts with 'a' and ends with 'e'YesPrint line
7beeDoes not start with 'a'NoSkip line
💡 All lines processed, grep ends after last line.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
Current Linenoneapplebananaaceanglegrapealebeenone
Match ResultnoneYesNoYesYesNoYesNonone
Output Lines[][apple][apple][apple, ace][apple, ace, angle][apple, ace, angle][apple, ace, angle, ale][apple, ace, angle, ale][apple, ace, angle, ale]
Key Moments - 3 Insights
Why does grep skip lines that do not start with 'a'?
Because the regex '^a.*e$' requires the line to start with 'a'. Lines like 'banana' fail this check as shown in execution_table row 2.
What does the '.*' in the regex mean?
'.*' means any characters (zero or more) between the start 'a' and the end 'e'. This allows lines like 'apple' and 'angle' to match, as seen in rows 1 and 4.
Why does grep print lines that end with 'e' only?
The regex ends with 'e$', meaning the line must end with 'e'. Lines like 'ale' match this, but 'bee' does not start with 'a' so it is skipped (row 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the match result for line 3 ('ace')?
ASkipped
BNo
CYes
DError
💡 Hint
Check row 3 under 'Match Result' column in execution_table.
At which step does grep skip a line because it does not start with 'a'?
AStep 2
BStep 1
CStep 4
DStep 6
💡 Hint
Look at 'Match Result' and 'Action' columns for lines not starting with 'a' in execution_table.
If the regex changed to '^b.*e$', which line would grep print at step 7?
Aangle
Bbee
Capple
Dale
💡 Hint
Check which lines start with 'b' and end with 'e' in the original lines from variable_tracker.
Concept Snapshot
grep with regex:
- Reads file line by line
- Checks each line against regex
- Prints lines matching regex
- '^' means start of line
- '$' means end of line
- '.*' means any characters in between
Full Transcript
This visual trace shows how grep uses a basic regex to filter lines in a file. The regex '^a.*e$' means lines must start with 'a' and end with 'e'. Each line is checked in order. Lines matching the pattern are printed; others are skipped. Variables track the current line, match result, and output lines. Key moments clarify why some lines match or not. The quiz tests understanding of matching and skipping behavior.