0
0
Linux CLIscripting~10 mins

grep (search text patterns) in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - grep (search text patterns)
Start grep command
Read input text line
Check if line matches pattern?
NoSkip line
Yes
Print matching line
More lines?
YesRead next line
No
End
grep reads each line of input, checks if it matches the pattern, prints it if yes, and repeats until all lines are processed.
Execution Sample
Linux CLI
echo -e "apple\nbanana\napple pie" | grep apple
Searches lines containing 'apple' from the given input and prints them.
Execution Table
StepInput LinePattern Match?ActionOutput
1appleYesPrint lineapple
2bananaNoSkip lineapple
3apple pieYesPrint lineapple apple pie
4No more lines-End-
💡 No more lines to read, grep finishes execution.
Variable Tracker
VariableStartAfter 1After 2After 3Final
Current Line-applebananaapple pieNo more lines
Outputappleappleapple apple pieapple apple pie
Key Moments - 2 Insights
Why does grep print only some lines and skip others?
grep prints lines only if they match the pattern, as shown in execution_table rows 1 and 3 where 'apple' is found, but skips row 2 where 'banana' does not match.
What happens when grep reaches the end of input?
As in execution_table row 4, when no more lines are available, grep stops reading and ends execution.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 2?
A"apple\napple pie"
B"apple"
C""
D"banana"
💡 Hint
Check the Output column after step 2 in the execution_table.
At which step does grep skip printing a line?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for 'Skip line' in the Action column of execution_table.
If the pattern was 'banana', which step would print output?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Check which input line matches 'banana' in the Input Line column.
Concept Snapshot
grep command searches text line by line.
It prints lines matching the given pattern.
Non-matching lines are skipped.
Stops when no more lines remain.
Syntax: grep pattern [file or input]
Useful for quick text search in files or streams.
Full Transcript
The grep command reads input text line by line. For each line, it checks if the line contains the search pattern. If yes, grep prints that line. If not, it skips it. This repeats until all lines are processed. For example, given three lines: 'apple', 'banana', and 'apple pie', searching for 'apple' prints the first and third lines only. When no more lines remain, grep ends. This simple flow helps quickly find text patterns in files or command outputs.