Bird
0
0

Which of the following is the correct syntax to replace all occurrences of 'cat' with 'dog' in a file pets.txt using sed?

easy📝 Syntax Q3 of 15
Linux CLI - Text Processing
Which of the following is the correct syntax to replace all occurrences of 'cat' with 'dog' in a file pets.txt using sed?
Ased 's/cat/dog/g' pets.txt
Bsed 's/cat/dog/' pets.txt
Csed 'replace cat with dog' pets.txt
Dsed 's/cat/dog/gp' pets.txt
Step-by-Step Solution
Solution:
  1. Step 1: Understand sed substitution flags

    The 'g' flag after substitution means replace all occurrences in each line, not just the first.
  2. Step 2: Identify correct syntax

    sed 's/cat/dog/g' pets.txt uses 's/cat/dog/g' which replaces all 'cat' with 'dog'. sed 's/cat/dog/' pets.txt replaces only first occurrence. sed 'replace cat with dog' pets.txt is invalid. sed 's/cat/dog/gp' pets.txt adds 'p' which prints lines but is not needed here.
  3. Final Answer:

    sed 's/cat/dog/g' pets.txt -> Option A
  4. Quick Check:

    Use 'g' flag for global replacement in sed [OK]
Quick Trick: Add 'g' flag to replace all matches per line [OK]
Common Mistakes:
  • Omitting 'g' flag to replace all occurrences
  • Using invalid commands like 'replace'
  • Confusing print flag 'p' with substitution

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Linux CLI Quizzes