0
0
Bash Scriptingscripting~20 mins

Quantifiers (*, +, ?) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Regex Quantifier 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 using the * quantifier?
Consider a file named data.txt with the following content:
apple
appple
aple
appppple

What will be the output of the command grep -E 'ap*le' data.txt?
Bash Scripting
grep -E 'ap*le' data.txt
A
aple
appple
B
apple
aple
C
apple
appple
appppple
D
apple
appple
aple
appppple
Attempts:
2 left
💡 Hint
Remember, * means zero or more of the previous character.
💻 Command Output
intermediate
2:00remaining
What does the + quantifier do in this grep command?
Given a file words.txt with content:
cat
caat
ct
caaat

What will be the output of grep -E 'ca+t' words.txt?
Bash Scripting
grep -E 'ca+t' words.txt
A
cat
caat
caaat
B
cat
ct
C
cat
caat
ct
caaat
D
caat
caaat
Attempts:
2 left
💡 Hint
The + quantifier means one or more of the previous character.
💻 Command Output
advanced
2:00remaining
What is the output of this grep command using the ? quantifier?
File input.txt contains:
color
colour
colr
colouur

What will grep -E 'colou?r' input.txt output?
Bash Scripting
grep -E 'colou?r' input.txt
A
color
colour
colouur
B
colour
colouur
C
color
colour
D
color
colr
Attempts:
2 left
💡 Hint
The ? quantifier means zero or one of the previous character.
🔧 Debug
advanced
2:00remaining
Why does this grep command fail to match expected lines?
You run grep -E 'ab+c*' file.txt on a file with lines:
abc
abbc
ac
abcc

But it matches 'abc', 'abbc', and 'abcc'. Why doesn't it match 'ac'?
Bash Scripting
grep -E 'ab+c*' file.txt
ABecause the pattern requires at least one 'b' and zero or more 'c's, so 'ac' is missing 'b'.
BBecause + quantifier is not supported in grep -E.
CBecause c* matches zero or more 'c's, so 'ac' does not match as 'b+' requires at least one 'b'.
DBecause the pattern matches only lines ending with 'c', so 'abcc' is excluded.
Attempts:
2 left
💡 Hint
Check what each quantifier means and what the pattern requires.
🧠 Conceptual
expert
3:00remaining
How many lines will match this grep pattern with mixed quantifiers?
File test.txt contains:
aaab
ab
aab
b
aaaab

How many lines will match grep -E 'a+b?a*' test.txt?
Bash Scripting
grep -E 'a+b?a*' test.txt
A5
B4
C2
D3
Attempts:
2 left
💡 Hint
Break down the pattern: a+ then b? then a*.