0
0
Bash Scriptingscripting~20 mins

Basic regex in grep in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Regex Grep Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this grep command?
Given a file fruits.txt with the following lines:
apple
banana
apricot
blueberry
avocado

What is the output of this command?
grep '^a' fruits.txt
A
apple
apricot
avocado
B
apricot
avocado
C
apple
banana
apricot
blueberry
avocado
D
banana
blueberry
Attempts:
2 left
💡 Hint
The caret (^) matches the start of a line.
💻 Command Output
intermediate
2:00remaining
Which lines does this grep command select?
Given a file colors.txt with these lines:
red
blue
green
yellow
black

What lines will this command print?
grep 'e$' colors.txt
A
red
blue
green
yellow
black
B
blue
green
Cblack
D
red
yellow
Attempts:
2 left
💡 Hint
The dollar sign ($) matches the end of a line.
💻 Command Output
advanced
2:00remaining
What is the output of this grep command with character classes?
Given a file words.txt containing:
cat
cot
cut
cit
cet

What lines will this command print?
grep 'c[aeiou]t' words.txt
A
cat
cot
cut
cet
B
cat
cot
cut
C
cat
cot
cut
cit
cet
D
cat
cot
cut
cit
Attempts:
2 left
💡 Hint
The pattern matches 'c' followed by a vowel, then 't'.
💻 Command Output
advanced
2:00remaining
What error or output does this grep command produce?
What happens when you run this command?
grep '[a-z' file.txt
ANo output, command runs silently
BPrints all lines containing any lowercase letter
CSyntax error: missing closing bracket in regex
DPrints lines containing the exact string '[a-z'
Attempts:
2 left
💡 Hint
Unclosed character classes are treated literally by grep.
🧠 Conceptual
expert
3:00remaining
How many lines does this grep command select?
Given a file data.txt with these lines:
123abc
abc123
abc
123
ab123cd
xyz

How many lines will this command print?
grep -E '^[0-9]+[a-z]+$' data.txt
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
The pattern means: start with one or more digits, then one or more lowercase letters, and nothing else.