Linux Foundation Certified System Administrator (LFCS) — Question 38
Which of the following commands replaces each occurrence of 'bob' in the file letter with 'Bob' and writes the result to the file newletter?
Answer options
- A. sed '/bob/Bob' letter > newletter
- B. sed s/bob/Bob/ letter < newletter
- C. sed 's/bob/Bob' letter > newletter
- D. sed 's/bob/Bob/g' letter > newletter
- E. sed 's/bob, Bob/' letter > newletter
Correct answer: D
Explanation
The correct answer, D, uses the 'g' flag to ensure all occurrences of 'bob' are replaced with 'Bob' in the file letter. Option A is incorrectly formatted and does not perform the replacement as needed. Option B attempts to use input redirection incorrectly and will not achieve the desired outcome. Option C replaces only the first occurrence rather than all, and option E has a syntax error with the comma, leading to an incorrect replacement pattern.