LX0-103 · Question #204
Which of the following commands replaces each occurrence of 'bob' in the file letter with 'Bob' and writes the result to the file newletter?
The correct answer is D. sed 's/bob/Bob/g' letter > newletter. The sed substitution command requires proper syntax 's/pattern/replacement/flags' and the 'g' flag to replace all occurrences globally, with output redirected to the target file.
Question
Which of the following commands replaces each occurrence of 'bob' in the file letter with 'Bob' and writes the result to the file newletter?
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
(33 responses)- B3% (1)
- C3% (1)
- D94% (31)
Why each option
The sed substitution command requires proper syntax 's/pattern/replacement/flags' and the 'g' flag to replace all occurrences globally, with output redirected to the target file.
The expression '/bob/Bob/' is missing the leading 's' command, making it an invalid sed address range rather than a substitution.
The redirection operator '<' reads from newletter as input instead of writing to it, and the substitution is missing the 'g' flag and uses incorrect syntax.
The substitution expression 's/bob/Bob' is missing the closing delimiter '/', making it syntactically incomplete and causing a sed error.
The command 'sed 's/bob/Bob/g' letter > newletter' uses the correct sed substitution syntax with all three delimiters present, and the 'g' flag ensures every occurrence of 'bob' on each line is replaced. Output is redirected with '>' to write the result to newletter.
The expression 's/bob, Bob/' contains a comma after 'bob' making it match a literal comma, and the replacement field is empty, so nothing meaningful is substituted.
Concept tested: sed global substitution syntax and output redirection
Source: https://www.gnu.org/software/sed/manual/sed.html
Topics
Community Discussion
No community discussion yet for this question.