nerdexam
CompTIA

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.

GNU and Unix Commands

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)
  • B
    3% (1)
  • C
    3% (1)
  • D
    94% (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.

Ased '/bob/Bob' letter > newletter

The expression '/bob/Bob/' is missing the leading 's' command, making it an invalid sed address range rather than a substitution.

Bsed s/bob/Bob/ letter < newletter

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.

Csed 's/bob/Bob' letter > newletter

The substitution expression 's/bob/Bob' is missing the closing delimiter '/', making it syntactically incomplete and causing a sed error.

Dsed 's/bob/Bob/g' letter > newletterCorrect

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.

Esed 's/bob, Bob/' letter > 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

#sed#text substitution#stream editor#regex

Community Discussion

No community discussion yet for this question.

Full LX0-103 Practice