0
0
Bash Scriptingscripting~20 mins

Character classes ([a-z], [0-9]) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Character Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of grep with character class [a-z]
What is the output of this command when run on a file containing the lines:

apple
Banana
cherry
123
date

grep '^[a-z]' filename

Assume the file is named filename and contains exactly the lines above.
Bash Scripting
grep '^[a-z]' filename
A
apple
Banana
cherry
date
B
apple
Banana
cherry
123
date
C
Banana
123
D
apple
cherry
date
Attempts:
2 left
💡 Hint
The pattern '^[a-z]' matches lines starting with a lowercase letter.
💻 Command Output
intermediate
2:00remaining
Matching digits with grep [0-9]
Given a file numbers.txt with the following lines:

abc123
456def
789
no_digits
123abc456

What lines will this command output?

grep '^[0-9]' numbers.txt
Bash Scripting
grep '^[0-9]' numbers.txt
A
456def
789
123abc456
B
abc123
456def
789
123abc456
C
789
no_digits
D
abc123
no_digits
Attempts:
2 left
💡 Hint
The pattern '^[0-9]' matches lines starting with a digit.
🔧 Debug
advanced
2:00remaining
Why does this grep command fail to match digits?
You run this command:

grep '[0-9]$' file.txt

But it does not match lines ending with digits as expected. What is the most likely reason?
Bash Scripting
grep '[0-9]$' file.txt
AThe character class [0-9] is invalid in grep.
BThe pattern is missing a caret ^ at the start.
CThe file has Windows line endings, so the $ matches before the carriage return, not the digit.
DThe file is empty, so no lines match.
Attempts:
2 left
💡 Hint
Check the file line endings if the pattern looks correct.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this character class usage
Which option contains a syntax error in the grep pattern using character classes?
Bash Scripting
grep '^[a-z0-9' file.txt
Agrep '^[a-z0-9' file.txt
Bgrep '^[a-z0-9-' file.txt
Cgrep '^[a-z0-9]' file.txt
Dtxt.elif ']9-0z-a[^' perg
Attempts:
2 left
💡 Hint
Check if the character class is properly closed with a bracket.
🚀 Application
expert
2:00remaining
Count lines starting with lowercase letters and digits
You want to count how many lines in data.txt start with either a lowercase letter or a digit. Which command will give the correct count?
Agrep -c '^[a-z]|^[0-9]' data.txt
Bgrep -c '^[a-z0-9]' data.txt
Cgrep -c '^[a-z][0-9]' data.txt
Dgrep -c '^[a-z0-9]$' data.txt
Attempts:
2 left
💡 Hint
Use a single character class to match either lowercase letters or digits at line start.