Bird
0
0

You want to replace all occurrences of 'foo' with 'bar' only on lines containing 'baz' in data.txt. Which command achieves this?

hard📝 Application Q15 of 15
Linux CLI - Text Processing
You want to replace all occurrences of 'foo' with 'bar' only on lines containing 'baz' in data.txt. Which command achieves this?
Ased '/baz/ s/foo/bar/g' data.txt
Bsed 's/foo/bar/g /baz/' data.txt
Csed 's/foo/bar/g' data.txt | grep baz
Dsed '/foo/ s/baz/bar/g' data.txt
Step-by-Step Solution
Solution:
  1. Step 1: Understand address restriction in sed

    Using /baz/ s/foo/bar/g applies substitution only on lines matching 'baz'.
  2. Step 2: Verify command correctness

    sed '/baz/ s/foo/bar/g' data.txt correctly restricts substitution to lines with 'baz' and replaces all 'foo' with 'bar'.
  3. Final Answer:

    sed '/baz/ s/foo/bar/g' data.txt -> Option A
  4. Quick Check:

    Address before command limits lines affected [OK]
Quick Trick: Use /pattern/ before s/// to limit lines [OK]
Common Mistakes:
  • Placing substitution after file name
  • Using grep after sed instead of address
  • Mixing patterns in wrong order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Linux CLI Quizzes