Bird
Raised Fist0
Linux CLIscripting~20 mins

grep (search text patterns) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Grep Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of grep with line numbers
Given a file data.txt containing the lines:
apple
banana
apple pie
pineapple
banana split

What is the output of this command?
grep -n 'apple' data.txt
A
2:banana
5:banana split
B
apple
apple pie
pineapple
C
1:apple
3:apple pie
4:pineapple
D
1:apple
2:banana
3:apple pie
4:pineapple
5:banana split
Attempts:
2 left
💡 Hint
The -n option shows line numbers of matching lines.
💻 Command Output
intermediate
2:00remaining
Using grep with word boundaries
Consider a file words.txt with these lines:
cat
cater
scatter
concatenate
cat
category

What is the output of:
grep -w 'cat' words.txt
A
cat
cat
B
cat
cater
scatter
concatenate
cat
category
C
cater
scatter
concatenate
category
D
cat
scatter
cat
Attempts:
2 left
💡 Hint
The -w option matches whole words only.
🔧 Debug
advanced
2:00remaining
Why does this grep command fail?
You run this command:
grep -e apple -e banana fruits.txt

But it returns no output even though the file contains those words. Which option explains the problem?
AThe command needs quotes around the patterns.
BThe <code>-e</code> option cannot be used multiple times.
CThe command requires the <code>-i</code> option to work.
DThe file name is incorrect or the file does not exist.
Attempts:
2 left
💡 Hint
Check if the file is accessible and named correctly.
💻 Command Output
advanced
2:00remaining
Output of grep with inverted match and count
Given a file colors.txt with:
red
green
blue
yellow
red
blue

What is the output of:
grep -vc 'red' colors.txt
A4
B2
C6
D0
Attempts:
2 left
💡 Hint
The -v option inverts the match; -c counts matching lines.
🚀 Application
expert
3:00remaining
Find lines with exactly 3 digits using grep
You want to find lines in log.txt that contain exactly three digits anywhere in the line (not more, not less). Which grep command achieves this?
Agrep -P '^(?:[^\d]*\d){3}[^\d]*$' log.txt
Bgrep -P '^(?:\D*\d){3}\D*$' log.txt
Cgrep -P '^([^\d]*\d){3}[^\d]*$' log.txt
Dgrep -P '^(?:\D*\d){3}\D*\d' log.txt
Attempts:
2 left
💡 Hint
Use Perl regex with -P and match exactly three digits with non-digit characters around.

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