Bird
0
0

You want to replace the word 'cat' with 'dog' only on lines 2 to 4 in a file named pets.txt. Which sed command achieves this?

hard📝 Application Q15 of 15
Linux CLI - Text Processing
You want to replace the word 'cat' with 'dog' only on lines 2 to 4 in a file named pets.txt. Which sed command achieves this?
Ased 's/cat/dog/ 2,4' pets.txt
Bsed '/2,4 s/cat/dog/' pets.txt
Csed '2,4s/cat/dog/' pets.txt
Dsed 's/cat/dog/ 2-4' pets.txt
Step-by-Step Solution
Solution:
  1. Step 1: Understand address range in sed

    To apply a command only on specific lines, use start,endcommand syntax, e.g., 2,4s/pattern/replacement/.
  2. Step 2: Match the correct syntax

    sed '2,4s/cat/dog/' pets.txt correctly uses 2,4s/cat/dog/ to replace 'cat' with 'dog' on lines 2 to 4.
  3. Final Answer:

    sed '2,4s/cat/dog/' pets.txt -> Option C
  4. Quick Check:

    Use line range before command to limit sed action [OK]
Quick Trick: Use 'start,end' before 's' to limit substitution lines [OK]
Common Mistakes:
  • Placing line numbers after substitution command
  • Using incorrect range syntax like '2-4'
  • Adding slashes around line numbers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Linux CLI Quizzes