LFCS Exam QuestionsBrowse all questions from this exam

LFCS Exam - Question 67


Which of the following commands replaces each occurrence of 'bob' in the file letter with 'Bob' and writes the result to the file newletter?

Show Answer
Correct Answer: D

To replace each occurrence of 'bob' in the file 'letter' with 'Bob' and write the result to the file 'newletter', the correct command is sed 's/bob/Bob/g' letter > newletter. In this command, 's' is the substitute command in sed, 'bob' is the text to be replaced, 'Bob' is the replacement text, and 'g' stands for global replacement which ensures all occurrences on each line are replaced. The '>' operator redirects the output to the new file 'newletter'.

Discussion

2 comments
Sign in to comment
StribOption: D
Jun 16, 2023

D. sed 's/bob/Bob/g' letter > newletter Explanation: The sed command is a stream editor used for modifying and transforming text. The s/old/new/g syntax is used in sed to substitute the first occurrence or all occurrences of "old" with "new" in a given input. In this case, the command sed 's/bob/Bob/g' letter > newletter is the correct choice. It reads the contents of the file "letter," replaces each occurrence of "bob" (case-sensitive) with "Bob," and then redirects the output to the file "newletter." The "g" at the end of the command is used as a flag to indicate a global replacement, meaning it replaces all occurrences of "bob" on each line of the file. Option D is the correct command for replacing each occurrence of 'bob' with 'Bob' in the file "letter" and writing the result to the file "newletter."

rhylosOption: D
Jul 11, 2021

Correct. student@centos ~]$ cat letter bob bob and john [student@centos ~]$ sed 's/bob/Bob/g' letter > newletter [student@centos ~]$ cat newletter Bob Bob and john