0
0
Bash Scriptingscripting~20 mins

Extended regex (grep -E) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Extended Regex Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of grep -E with alternation
What is the output of this command when run on a file containing these lines:
apple
banana
apricot
blueberry
blackberry

Command:
grep -E 'ap|bl' filename
Bash Scripting
grep -E 'ap|bl' filename
A
apple
apricot
blueberry
blackberry
B
apple
banana
apricot
blueberry
blackberry
C
banana
blueberry
blackberry
D
apple
apricot
Attempts:
2 left
💡 Hint
Look for lines containing either 'ap' or 'bl' anywhere in the line.
💻 Command Output
intermediate
2:00remaining
Using grep -E with character classes
Given a file with these lines:
cat
cot
cut
cit
cet

What is the output of:
grep -E 'c[aeiou]t' filename
Bash Scripting
grep -E 'c[aeiou]t' filename
A
cat
cot
cut
B
cat
cot
cut
cet
C
cat
cot
cut
cit
cet
D
cat
cit
cet
Attempts:
2 left
💡 Hint
The character class [aeiou] matches any vowel.
💻 Command Output
advanced
2:00remaining
Output of grep -E with quantifiers
What is the output of this command on a file with these lines:
aaa
aa
aaaa
a
aaaaa

Command:
grep -E 'a{3,4}' filename
Bash Scripting
grep -E 'a{3,4}' filename
A
aaa
aaaa
aaaaa
B
aaa
aaaa
C
aaaa
aaaaa
Daaa
Attempts:
2 left
💡 Hint
The pattern matches sequences of 'a' repeated 3 or 4 times anywhere in the line.
💻 Command Output
advanced
2:00remaining
grep -E with anchors and groups
Given a file with these lines:
foo123
123foo
foo456
bar123foo
foo

What is the output of:
grep -E '^foo[0-9]+$' filename
Bash Scripting
grep -E '^foo[0-9]+$' filename
A
foo123
123foo
foo456
B
foo123
foo456
C
foo123
foo456
foo
D
foo
foo123
foo456
Attempts:
2 left
💡 Hint
The pattern matches lines starting with 'foo' and ending with one or more digits.
💻 Command Output
expert
2:00remaining
Complex pattern with grep -E and lookalike syntax
What error or output results from running this command on any file?
grep -E '(?<=foo)bar' filename
Bash Scripting
grep -E '(?<=foo)bar' filename
ANo output, no error
BPrints lines containing 'foobar'
CPrints lines containing 'bar' only if preceded by 'foo'
DSyntax error: invalid regular expression
Attempts:
2 left
💡 Hint
grep -E does not support lookbehind assertions like (?<=...).