Bird
0
0

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

easy📝 Conceptual Q11 of 15
Linux CLI - Text Processing
What does the sed command sed 's/cat/dog/' file.txt do?
ADeletes all lines containing 'cat' in file.txt
BReplaces the first occurrence of 'cat' with 'dog' on each line of file.txt
CPrints only lines containing 'dog' from file.txt
DAppends 'dog' at the end of each line containing 'cat' in file.txt
Step-by-Step Solution
Solution:
  1. Step 1: Understand the substitution command

    The s/pattern/replacement/ syntax in sed replaces the first match of 'pattern' with 'replacement' on each line.
  2. Step 2: Apply to the given command

    The command replaces the first occurrence of 'cat' with 'dog' on every line of file.txt.
  3. Final Answer:

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

    sed substitution = replace first match [OK]
Quick Trick: Remember: s/pattern/replacement/ changes first match per line [OK]
Common Mistakes:
  • Thinking it deletes lines instead of substituting
  • Assuming it replaces all occurrences without 'g' flag
  • Confusing printing lines with substitution

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Linux CLI Quizzes