0
0
Bash Scriptingscripting~20 mins

Anchors (^, $) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Regex Anchor 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 data.txt with the following lines:
apple
application
pineapple
snapple
apple pie

What is the output of this command?
grep '^apple$' data.txt
Bash Scripting
grep '^apple$' data.txt
A
apple
application
apple pie
B
apple
apple pie
Capple
Dapple pie
Attempts:
2 left
💡 Hint
The ^ anchor matches the start of the line, and $ matches the end of the line.
💻 Command Output
intermediate
2:00remaining
Which lines does this grep command print?
Given a file log.txt with these lines:
error: file not found
warning: low disk space
error: access denied
info: update complete

What lines will this command print?
grep '^error:' log.txt
Bash Scripting
grep '^error:' log.txt
Aerror: access denied
B
error: file not found
error: access denied
C
error: file not found
warning: low disk space
error: access denied
D
file not found
access denied
Attempts:
2 left
💡 Hint
The ^ anchor matches the start of the line, so only lines starting with 'error:' are matched.
📝 Syntax
advanced
2:00remaining
Which grep pattern matches lines ending with '.txt'?
You want to find lines in a file that end exactly with '.txt'. Which grep pattern is correct?
Agrep '^txt$' filename
Bgrep '^\.txt' filename
Cgrep '\.txt' filename
Dgrep '\.txt$' filename
Attempts:
2 left
💡 Hint
The $ anchor matches the end of the line. The dot must be escaped to match a literal dot.
🔧 Debug
advanced
2:00remaining
Why does this grep command not match any lines?
You run this command:
grep '^error$' log.txt

But no lines are printed, even though the file has lines starting with 'error:'. Why?
ABecause '^error$' matches lines exactly equal to 'error', but lines have 'error:' with colon.
BBecause '^error$' matches lines starting with 'error' but not ending with it.
CBecause grep needs -E option to use anchors.
DBecause the file is empty.
Attempts:
2 left
💡 Hint
Check if the lines exactly match the pattern or have extra characters.
🚀 Application
expert
2:00remaining
How many lines match this grep pattern?
Given a file words.txt with these lines:
cat
concatenate
scatter
cater
act

How many lines will this command print?
grep '^cat$' words.txt
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
The pattern matches lines exactly equal to 'cat'.