LFCS · Question #714
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 command with the s/old/new/g substitution and global flag is used to replace all occurrences of a string within a file and redirect the output to a new file.
Question
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
(51 responses)- A4% (2)
- B2% (1)
- C8% (4)
- D73% (37)
- E14% (7)
Why each option
The `sed` command with the `s/old/new/g` substitution and global flag is used to replace all occurrences of a string within a file and redirect the output to a new file.
The syntax `/bob/Bob` is incomplete for substitution in `sed`; it would attempt to print lines matching 'bob', not perform a replacement.
The input and output redirection are swapped (`< newletter` when `letter` should be the input), and the command syntax `sed s/bob/Bob/letter` is missing the quotes and global flag.
This command `sed 's/bob/Bob'` lacks the global (`g`) flag, so it would only replace the first occurrence of 'bob' on each line, not all instances.
The `sed` command `s/bob/Bob/g` performs a substitution where `s` initiates the command, `bob` is the pattern to find, `Bob` is the replacement string, and the `g` flag ensures all occurrences on each line are replaced. The output redirection `> newletter` creates a new file with the changes.
The substitution pattern `bob. Bob/` contains a literal dot and space, and is syntactically incorrect without the `s` command and proper delimiters.
Concept tested: sed command for global substitution
Source: https://www.gnu.org/software/sed/manual/sed.html#sed-commands-list
Topics
Community Discussion
No community discussion yet for this question.