nerdexam
CompTIA

LX0-103 · Question #114

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 substitution command requires the syntax 's/pattern/replacement/g' with the g flag for global replacement and '>' to redirect output to a new file.

GNU and Unix Commands

Question

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" with "Bob" in all instances and generate a new letter for printing?

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

(39 responses)
  • A
    8% (3)
  • C
    3% (1)
  • D
    85% (33)
  • E
    5% (2)

Why each option

The sed substitution command requires the syntax 's/pattern/replacement/g' with the g flag for global replacement and '>' to redirect output to a new file.

Ased '/bob/Bob' letter >newletter

This omits the required 's/' command prefix and the 'g' flag, making it an invalid sed expression.

Bsed s/bob/Bob/letter < newletter

This reverses the redirection operator (using '<' instead of '>') and omits the 's/' command prefix, producing incorrect syntax.

Csed 's/bob/Bob' letter > newletter

The substitution pattern is missing its closing '/' delimiter and the 'g' flag, so sed would throw a syntax error and no global replacement would occur.

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

sed 's/bob/Bob/g' letter > newletter uses the correct sed substitution syntax: the 's/' command prefix, the source pattern 'bob', the replacement 'Bob', and the 'g' (global) flag to replace every occurrence on each line rather than just the first. The shell redirection '>' writes the transformed output to newletter without modifying the original file.

Esed 's/bob. Bob/' letter > newletter

The pattern 's/bob. Bob/' is malformed - it contains a literal dot and a space inside the pattern and has no replacement field after the second delimiter, making it invalid sed syntax.

Concept tested: sed global substitution syntax and output redirection

Source: https://www.gnu.org/software/sed/manual/sed.html#The-s-command

Topics

#sed#global substitution#text processing#stream editor

Community Discussion

No community discussion yet for this question.

Full LX0-103 Practice