Challenge - 5 Problems
Grep Guru
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of grep with multiple options
What is the output of this bash command when run on a file named
Command:
data.txt containing the lines:apple banana Apple pie banana split APPLE
Command:
grep -i 'apple' data.txt
Bash Scripting
grep -i 'apple' data.txtAttempts:
2 left
💡 Hint
The
-i option makes grep ignore case when matching.✗ Incorrect
The
-i option matches 'apple' regardless of case, so it finds 'apple', 'Apple pie', and 'APPLE'. Lines without 'apple' are ignored.💻 Command Output
intermediate2:00remaining
Using grep to count matching lines
Given a file
log.txt with 10 lines, 3 of which contain the word 'error', what is the output of:grep -c 'error' log.txt
Bash Scripting
grep -c 'error' log.txtAttempts:
2 left
💡 Hint
The
-c option counts matching lines instead of printing them.✗ Incorrect
The
-c option returns the number of lines that contain the pattern. Since 3 lines have 'error', the output is 3.🔧 Debug
advanced2:00remaining
Why does this grep command fail?
Consider this script snippet:
Why might this script not print 'Found' even if 'pattern' is in the file?
if grep 'pattern' file.txt then echo 'Found' fi
Why might this script not print 'Found' even if 'pattern' is in the file?
Bash Scripting
if grep 'pattern' file.txt then echo 'Found' fi
Attempts:
2 left
💡 Hint
Check how grep exit status works in if conditions.
✗ Incorrect
grep returns exit status 0 if it finds the pattern, so the if condition is true and 'Found' is printed. The script works as intended.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this grep usage
Which option shows the correct syntax to search for lines containing 'test' in
file.txt and ignore case?Attempts:
2 left
💡 Hint
Check how to pass the pattern and filename correctly.
✗ Incorrect
Option A correctly uses quotes around the pattern and specifies the filename after the pattern. Option A works but is less safe without quotes. Options C and D have syntax errors.
🚀 Application
expert3:00remaining
Extract lines with numbers and save count
You want to write a bash script that extracts all lines containing digits from
input.txt and saves them to numbers.txt. Then, you want to store the count of these lines in a variable count. Which snippet does this correctly?Attempts:
2 left
💡 Hint
Use a command that both saves output and counts lines in a single pipeline.
✗ Incorrect
Option C uses tee to save grep output to file and pipe it to wc -l to count lines, storing count in variable. Option C incorrectly pipes after redirect, so wc -l counts nothing. Option C requires two separate commands. Option C saves count to file, not lines.