Bird
Raised Fist0
Linux CLIscripting~10 mins

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

Choose your learning style10 modes available
Concept Flow - grep (search text patterns)
Start grep command
↓
Read input text line
↓
Check if line matches pattern?
No→Skip line
Yes↓
Print matching line
↓
More lines?
Yes→Read 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.

Practice

(1/5)
1. What does the grep command do in Linux?
easy
A. Searches text for lines matching a pattern
B. Deletes files matching a pattern
C. Copies files to another directory
D. Lists all files in a directory

Solution

  1. Step 1: Understand the purpose of grep

    The grep command is used to search text for lines that match a given pattern.
  2. Step 2: Compare options

    Options B, C, and D describe other commands like rm, cp, and ls, not grep.
  3. Final Answer:

    Searches text for lines matching a pattern -> Option A
  4. Quick Check:

    grep = search text lines [OK]
Hint: grep finds lines with your pattern in text [OK]
Common Mistakes:
  • Confusing grep with file commands like rm or cp
  • Thinking grep modifies files instead of searching
  • Assuming grep lists files instead of searching content
2. Which of the following is the correct syntax to search for the word error in a file named log.txt using grep?
easy
A. grep log.txt error
B. grep -f error log.txt
C. grep error log.txt
D. grep -e log.txt error

Solution

  1. Step 1: Recall grep syntax

    The basic syntax is grep [pattern] [file]. So to search for 'error' in 'log.txt', use grep error log.txt.
  2. Step 2: Analyze other options

    grep -f error log.txt uses -f which expects a file of patterns, not a pattern string. Options C and D have arguments reversed or incorrect flags.
  3. Final Answer:

    grep error log.txt -> Option C
  4. Quick Check:

    grep pattern file = grep error log.txt [OK]
Hint: Use grep pattern filename order to avoid errors [OK]
Common Mistakes:
  • Swapping pattern and filename positions
  • Using -f flag incorrectly for pattern string
  • Confusing -e flag usage
3. Given a file data.txt with the following content:
apple
banana
Apple
BANANA
apple pie
banana split

What is the output of the command grep -i 'apple' data.txt?
medium
A. apple apple pie
B. Apple BANANA banana split
C. apple pie banana split
D. apple Apple apple pie

Solution

  1. Step 1: Understand -i option

    The -i option makes grep case-insensitive, so it matches 'apple' and 'Apple'.
  2. Step 2: Identify matching lines

    Lines containing 'apple' ignoring case are: 'apple', 'Apple', and 'apple pie'.
  3. Final Answer:

    apple Apple apple pie -> Option D
  4. Quick Check:

    grep -i matches apple and Apple [OK]
Hint: Use -i to ignore case when searching text [OK]
Common Mistakes:
  • Ignoring case sensitivity without -i
  • Missing lines with partial matches like 'apple pie'
  • Confusing output order
4. You run the command grep -c 'fail' report.txt but it returns 0 even though you know the file contains the word 'fail'. What is the most likely reason?
medium
A. The file report.txt is empty
B. The word 'fail' is in uppercase like 'FAIL' and grep is case-sensitive by default
C. The grep command syntax is incorrect
D. The file report.txt does not exist

Solution

  1. Step 1: Understand grep -c behavior

    The -c option counts matching lines. If it returns 0, no exact matches were found.
  2. Step 2: Consider case sensitivity

    By default, grep is case-sensitive. If the file has 'FAIL' but you search 'fail', it won't match.
  3. Final Answer:

    The word 'fail' is in uppercase like 'FAIL' and grep is case-sensitive by default -> Option B
  4. Quick Check:

    grep is case-sensitive unless -i used [OK]
Hint: Use -i to ignore case if matches return zero [OK]
Common Mistakes:
  • Assuming grep ignores case by default
  • Not checking file content carefully
  • Mistaking syntax errors for no matches
5. You want to find how many lines in server.log contain the word timeout ignoring case, and display only the count. Which command should you use?
hard
A. grep -i -c timeout server.log
B. grep -i timeout server.log
C. grep timeout -i -c server.log
D. grep -c timeout server.log

Solution

  1. Step 1: Identify options needed

    You need to ignore case (-i) and count matching lines (-c).
  2. Step 2: Check correct option order

    Options can be combined as -i -c or -c -i. grep -i -c timeout server.log uses both correctly before pattern and file.
  3. Step 3: Analyze other options

    grep -c timeout server.log lacks case insensitivity. grep -i timeout server.log shows matches but not count. grep timeout -i -c server.log has incorrect argument order (pattern before options).
  4. Final Answer:

    grep -i -c timeout server.log -> Option A
  5. Quick Check:

    Use -i and -c together for case-insensitive count [OK]
Hint: Combine -i and -c to count ignoring case [OK]
Common Mistakes:
  • Omitting -i for case-insensitive search
  • Using pattern before options causing errors
  • Expecting output lines instead of count
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