Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Basic regex in grep in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Regex Grep 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 fruits.txt with the following lines:
apple
banana
apricot
blueberry
avocado

What is the output of this command?
grep '^a' fruits.txt
A
apple
apricot
avocado
B
apricot
avocado
C
apple
banana
apricot
blueberry
avocado
D
banana
blueberry
Attempts:
2 left
💡 Hint
The caret (^) matches the start of a line.
💻 Command Output
intermediate
2:00remaining
Which lines does this grep command select?
Given a file colors.txt with these lines:
red
blue
green
yellow
black

What lines will this command print?
grep 'e$' colors.txt
A
red
blue
green
yellow
black
B
blue
green
Cblack
D
red
yellow
Attempts:
2 left
💡 Hint
The dollar sign ($) matches the end of a line.
💻 Command Output
advanced
2:00remaining
What is the output of this grep command with character classes?
Given a file words.txt containing:
cat
cot
cut
cit
cet

What lines will this command print?
grep 'c[aeiou]t' words.txt
A
cat
cot
cut
cet
B
cat
cot
cut
C
cat
cot
cut
cit
cet
D
cat
cot
cut
cit
Attempts:
2 left
💡 Hint
The pattern matches 'c' followed by a vowel, then 't'.
💻 Command Output
advanced
2:00remaining
What error or output does this grep command produce?
What happens when you run this command?
grep '[a-z' file.txt
ANo output, command runs silently
BPrints all lines containing any lowercase letter
CSyntax error: missing closing bracket in regex
DPrints lines containing the exact string '[a-z'
Attempts:
2 left
💡 Hint
Unclosed character classes are treated literally by grep.
🧠 Conceptual
expert
3:00remaining
How many lines does this grep command select?
Given a file data.txt with these lines:
123abc
abc123
abc
123
ab123cd
xyz

How many lines will this command print?
grep -E '^[0-9]+[a-z]+$' data.txt
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
The pattern means: start with one or more digits, then one or more lowercase letters, and nothing else.

Practice

(1/5)
1. What does the caret symbol ^ mean in a basic regex pattern used with grep?
easy
A. It matches the end of a line
B. It matches the start of a line
C. It matches any single character
D. It matches zero or more repetitions

Solution

  1. Step 1: Understand caret usage in regex

    The caret ^ is used to anchor the pattern to the start of a line in basic regex.
  2. Step 2: Compare with other anchors

    The dollar sign $ matches the end of a line, so caret is specifically for the start.
  3. Final Answer:

    It matches the start of a line -> Option B
  4. Quick Check:

    ^ means start of line [OK]
Hint: Caret (^) always means line start in grep regex [OK]
Common Mistakes:
  • Confusing ^ with $ (end of line)
  • Thinking ^ matches any character
  • Using ^ to mean OR condition
2. Which of the following is the correct basic regex syntax to search for lines containing either "cat" or "dog" using grep?
easy
A. grep 'cat\|dog' file.txt
B. grep 'cat|dog' file.txt
C. grep 'cat\dog' file.txt
D. grep 'cat/dog' file.txt

Solution

  1. Step 1: Understand OR in basic regex

    In basic regex with grep, the OR operator is written as \|, not just |.
  2. Step 2: Check each option

    grep 'cat|dog' file.txt uses | without escape, which is invalid in basic regex. grep 'cat\|dog' file.txt correctly escapes it as \|. Options C and D use wrong characters.
  3. Final Answer:

    grep 'cat\|dog' file.txt -> Option A
  4. Quick Check:

    OR in basic grep regex = \| [OK]
Hint: Use \| for OR in basic grep regex [OK]
Common Mistakes:
  • Using | without escaping in basic grep
  • Confusing \| with \dog or /
  • Forgetting to escape special characters
3. Given a file animals.txt with these lines:
cat
caterpillar
dog
catalog
dogma

What is the output of grep '^cat$' animals.txt?
medium
A. cat caterpillar catalog
B. caterpillar catalog
C. cat
D. No output

Solution

  1. Step 1: Understand the regex pattern

    The pattern ^cat$ matches lines that start and end exactly with "cat" (no extra characters).
  2. Step 2: Check each line against pattern

    Only the line "cat" matches exactly. "caterpillar" and "catalog" have extra characters, so they don't match.
  3. Final Answer:

    cat -> Option C
  4. Quick Check:

    ^cat$ matches exact line 'cat' [OK]
Hint: Use ^ and $ to match exact whole line [OK]
Common Mistakes:
  • Assuming partial matches with ^ and $
  • Ignoring line boundaries in regex
  • Confusing grep default substring match with anchored match
4. You want to find lines containing either "apple" or "banana" in fruits.txt using basic regex with grep. Which command uses incorrect syntax for the OR operation?
medium
A. grep 'apple|banana' fruits.txt
B. grep 'apple\|banana$' fruits.txt
C. grep '^apple\|banana$' fruits.txt
D. grep 'apple\|banana' fruits.txt

Solution

  1. Step 1: Identify correct OR syntax in basic grep

    Basic regex requires escaping the OR operator as \|. Using | alone treats it as a literal | character, not as an OR operator.
  2. Step 2: Check each option

    grep 'apple|banana' fruits.txt uses | without escape, treating | as literal, not OR. Options A, B, and C correctly escape OR operator.
  3. Final Answer:

    grep 'apple|banana' fruits.txt -> Option A
  4. Quick Check:

    Unescaped | in basic grep regex = literal |, not OR [OK]
Hint: Escape | as \| in basic grep regex [OK]
Common Mistakes:
  • Not escaping | in basic regex
  • Mixing extended regex syntax without -E flag
  • Misplacing anchors with OR operator
5. You want to find lines in data.txt that start with "error" or end with "fail" using basic regex with grep. Which command correctly does this?
hard
A. grep '^error\|fail' data.txt
B. grep '^error|fail$' data.txt
C. grep '^error$\|fail$' data.txt
D. grep '^error\|fail$' data.txt

Solution

  1. Step 1: Understand the pattern requirements

    We want lines starting with "error" OR lines ending with "fail". So pattern is ^error OR fail$.
  2. Step 2: Use correct OR syntax in basic regex

    Basic regex OR is \|. So pattern is ^error\|fail$. grep '^error\|fail$' data.txt matches this exactly.
  3. Step 3: Check other options

    grep '^error|fail$' data.txt misses escape on OR, causing error. grep '^error$\|fail$' data.txt incorrectly anchors "error" with $. grep '^error\|fail' data.txt misses anchor on "fail".
  4. Final Answer:

    grep '^error\|fail$' data.txt -> Option D
  5. Quick Check:

    Start with ^error OR end with fail$ using \| [OK]
Hint: Combine ^ and $ with \| for OR in basic grep regex [OK]
Common Mistakes:
  • Not escaping | in basic regex
  • Misplacing anchors with OR operator
  • Using $ with start anchor or vice versa
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard