Bird
0
0

What does the sed command sed 's/cat/dog/' file.txt do?

easy🧠 Conceptual Q11 of 15
Bash Scripting - Text Processing in Scripts
What does the sed command sed 's/cat/dog/' file.txt do?
APrints lines containing 'cat' from file.txt
BReplaces all occurrences of 'cat' with 'dog' in the entire file.txt
CReplaces the first occurrence of 'cat' with 'dog' on each line of file.txt
DDeletes all lines containing 'cat' from file.txt
Step-by-Step Solution
Solution:
  1. Step 1: Understand the sed substitution syntax

    The command sed 's/cat/dog/' file.txt uses s/old/new/ which replaces only the first match per line.
  2. Step 2: Apply to the file content

    Each line in file.txt will have its first 'cat' replaced by 'dog', other occurrences remain unchanged.
  3. Final Answer:

    Replaces the first occurrence of 'cat' with 'dog' on each line of file.txt -> Option C
  4. Quick Check:

    sed s/old/new/ replaces first match per line [OK]
Quick Trick: Remember: no 'g' means only first match replaced per line [OK]
Common Mistakes:
MISTAKES
  • Thinking it replaces all matches without 'g'
  • Confusing substitution with deletion
  • Assuming it prints only matching lines

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes