Challenge - 5 Problems
Grep Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of grep with line numbers
Given a file
What is the output of this command?
data.txt containing the lines:apple banana apple pie pineapple banana split
What is the output of this command?
grep -n 'apple' data.txt
Attempts:
2 left
💡 Hint
The
-n option shows line numbers of matching lines.✗ Incorrect
The command searches for lines containing 'apple' and shows their line numbers. Lines 1, 3, and 4 contain 'apple'.
💻 Command Output
intermediate2:00remaining
Using grep with word boundaries
Consider a file
What is the output of:
words.txt with these lines:cat cater scatter concatenate cat category
What is the output of:
grep -w 'cat' words.txt
Attempts:
2 left
💡 Hint
The
-w option matches whole words only.✗ Incorrect
Only lines where 'cat' appears as a whole word match. Lines with 'cater', 'scatter', etc. do not match.
🔧 Debug
advanced2:00remaining
Why does this grep command fail?
You run this command:
But it returns no output even though the file contains those words. Which option explains the problem?
grep -e apple -e banana fruits.txt
But it returns no output even though the file contains those words. Which option explains the problem?
Attempts:
2 left
💡 Hint
Check if the file is accessible and named correctly.
✗ Incorrect
Using multiple
-e options is valid. If no output appears, the file might be missing or named wrong.💻 Command Output
advanced2:00remaining
Output of grep with inverted match and count
Given a file
What is the output of:
colors.txt with:red green blue yellow red blue
What is the output of:
grep -vc 'red' colors.txt
Attempts:
2 left
💡 Hint
The
-v option inverts the match; -c counts matching lines.✗ Incorrect
Lines not containing 'red' are green, blue, yellow, blue (4 lines).
🚀 Application
expert3: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?Attempts:
2 left
💡 Hint
Use Perl regex with
-P and match exactly three digits with non-digit characters around.✗ Incorrect
Option B matches lines with exactly three digits by allowing any number of non-digit characters before, between, and after digits, but exactly three digits total.