Challenge - 5 Problems
Character Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2: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
Assume the file is named
apple
Banana
cherry
123
date
grep '^[a-z]' filenameAssume the file is named
filename and contains exactly the lines above.Bash Scripting
grep '^[a-z]' filenameAttempts:
2 left
💡 Hint
The pattern '^[a-z]' matches lines starting with a lowercase letter.
✗ Incorrect
The grep command with '^[a-z]' matches lines starting with any lowercase letter from a to z. Lines starting with uppercase letters or digits are excluded.
💻 Command Output
intermediate2:00remaining
Matching digits with grep [0-9]
Given a file
abc123
456def
789
no_digits
123abc456
What lines will this command output?
numbers.txt with the following lines:abc123
456def
789
no_digits
123abc456
What lines will this command output?
grep '^[0-9]' numbers.txtBash Scripting
grep '^[0-9]' numbers.txtAttempts:
2 left
💡 Hint
The pattern '^[0-9]' matches lines starting with a digit.
✗ Incorrect
Only lines starting with digits 0-9 are matched. So lines starting with letters or no digits at start are excluded.
🔧 Debug
advanced2:00remaining
Why does this grep command fail to match digits?
You run this command:
But it does not match lines ending with digits as expected. What is the most likely reason?
grep '[0-9]$' file.txtBut it does not match lines ending with digits as expected. What is the most likely reason?
Bash Scripting
grep '[0-9]$' file.txtAttempts:
2 left
💡 Hint
Check the file line endings if the pattern looks correct.
✗ Incorrect
If the file has Windows-style line endings (\r\n), the $ matches before the \r, so the digit is not at the end of the line, causing no match.
📝 Syntax
advanced2: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.txtAttempts:
2 left
💡 Hint
Check if the character class is properly closed with a bracket.
✗ Incorrect
Option A is missing the closing bracket ] for the character class, causing a syntax error.
🚀 Application
expert2: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?Attempts:
2 left
💡 Hint
Use a single character class to match either lowercase letters or digits at line start.
✗ Incorrect
Option B correctly uses a character class [a-z0-9] to match lines starting with either a lowercase letter or digit and counts them. Option B uses alternation incorrectly inside grep basic regex. Option B matches lines starting with a lowercase letter followed immediately by a digit. Option B matches lines with exactly one character that is lowercase letter or digit.