Bird
0
0

You wrote this sed command to replace 'foo' with 'bar' globally: sed 's/foo/bar/' file.txt. But only the first 'foo' per line is replaced. What is the fix?

medium📝 Debug Q14 of 15
Bash Scripting - Text Processing in Scripts
You wrote this sed command to replace 'foo' with 'bar' globally: sed 's/foo/bar/' file.txt. But only the first 'foo' per line is replaced. What is the fix?
AAdd the 'g' flag: sed 's/foo/bar/g' file.txt
BUse double quotes instead of single quotes
CAdd a space after 'bar/'
DUse sed -i option to edit in place
Step-by-Step Solution
Solution:
  1. Step 1: Identify missing global flag

    The command lacks the 'g' flag, so only first occurrence per line is replaced.
  2. Step 2: Add the 'g' flag to fix

    Adding 'g' after the substitution pattern makes sed replace all occurrences per line.
  3. Final Answer:

    Add the 'g' flag: sed 's/foo/bar/g' file.txt -> Option A
  4. Quick Check:

    Global flag 'g' replaces all matches [OK]
Quick Trick: Add 'g' to replace all matches, not just first [OK]
Common Mistakes:
MISTAKES
  • Thinking quotes affect substitution behavior
  • Adding spaces inside substitution pattern
  • Confusing in-place edit with substitution flags

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes