LX0-103 · Question #114
You have created a really long letter and after you are done you notice that you used the name "Bob" many times but you forgot to capitalize it in many instances. Which command would replace "bob"…
The correct answer is D. sed 's/bob/Bob/g' letter > newletter. The sed substitution command requires the syntax 's/pattern/replacement/g' with the g flag for global replacement and '>' to redirect output to a new file.
Question
You have created a really long letter and after you are done you notice that you used the name "Bob" many times but you forgot to capitalize it in many instances. Which command would replace "bob" with "Bob" in all instances and generate a new letter for printing?
Options
- Ased '/bob/Bob' letter >newletter
- Bsed s/bob/Bob/letter < newletter
- Csed 's/bob/Bob' letter > newletter
- Dsed 's/bob/Bob/g' letter > newletter
- Esed 's/bob. Bob/' letter > newletter
How the community answered
(39 responses)- A8% (3)
- C3% (1)
- D85% (33)
- E5% (2)
Why each option
The sed substitution command requires the syntax 's/pattern/replacement/g' with the g flag for global replacement and '>' to redirect output to a new file.
This omits the required 's/' command prefix and the 'g' flag, making it an invalid sed expression.
This reverses the redirection operator (using '<' instead of '>') and omits the 's/' command prefix, producing incorrect syntax.
The substitution pattern is missing its closing '/' delimiter and the 'g' flag, so sed would throw a syntax error and no global replacement would occur.
sed 's/bob/Bob/g' letter > newletter uses the correct sed substitution syntax: the 's/' command prefix, the source pattern 'bob', the replacement 'Bob', and the 'g' (global) flag to replace every occurrence on each line rather than just the first. The shell redirection '>' writes the transformed output to newletter without modifying the original file.
The pattern 's/bob. Bob/' is malformed - it contains a literal dot and a space inside the pattern and has no replacement field after the second delimiter, making it invalid sed syntax.
Concept tested: sed global substitution syntax and output redirection
Source: https://www.gnu.org/software/sed/manual/sed.html#The-s-command
Topics
Community Discussion
No community discussion yet for this question.