0
0
Linux CLIscripting~20 mins

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

Choose your learning style9 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.