nerdexam
CompTIA

XK0-004 · Question #256

A Linux administrator recently changed the IP addresses of all the web servers from 10.10.50.x to 10.10.100.x. The administrator needs to update the serverlist.txt file to reflect the change. The file

The correct answer is C. sed -i 's|.50|.100|' serverlist.txt. Updating a file in place with sed requires the -i flag, and since each web server line contains only one matching .50 pattern, the substitution without the g flag is the precise correct form.

Scripting, Containers and Automation

Question

A Linux administrator recently changed the IP addresses of all the web servers from 10.10.50.x to 10.10.100.x. The administrator needs to update the serverlist.txt file to reflect the change. The file contains the following:

10:WebSvr01:10.10.50.21:Main 11:WebSvr02:10.10.50.22:Acconting 12:WebSvr03:10.10.50.23:Intranet 20:NFS01:10.10.20.21:FileServer 30:SMTP01:10.10.30.31:Email Which of the following commands will change the IP addresses and update the files in place?

Options

  • Ased 's|.50|.100|g' serverlist.txt
  • Bsed -g 's|.50|.100|a' serverlist.txt
  • Csed -i 's|.50|.100|' serverlist.txt
  • Dsed -i 's|.50|.100|g' serverlist.txt

How the community answered

(29 responses)
  • A
    3% (1)
  • B
    7% (2)
  • C
    83% (24)
  • D
    7% (2)

Why each option

Updating a file in place with sed requires the -i flag, and since each web server line contains only one matching .50 pattern, the substitution without the g flag is the precise correct form.

Ased 's|.50|.100|g' serverlist.txt

Without the '-i' flag, sed prints the modified output to stdout instead of updating serverlist.txt in place, so the file itself is never changed.

Bsed -g 's|.50|.100|a' serverlist.txt

'-g' is not a valid sed flag; the global replacement modifier is specified as a trailing 'g' inside the substitution expression, not as a standalone command-line option, making this command syntactically invalid.

Csed -i 's|.50|.100|' serverlist.txtCorrect

The '-i' flag tells sed to edit the file in place, writing changes directly back to serverlist.txt rather than printing to stdout. The substitution 's|.50|.100|' replaces the first occurrence of '.50' per line, which is sufficient because each web server entry contains exactly one matching IP segment, correctly updating 10.10.50.x to 10.10.100.x.

Dsed -i 's|.50|.100|g' serverlist.txt

The 'g' flag instructs sed to replace all occurrences per line; because '.' in the regex pattern matches any character, the global flag risks unintended replacements if other substrings on a line incidentally match the pattern, making it less precise than the non-global form for this task.

Concept tested: In-place file substitution with sed -i

Source: https://www.gnu.org/software/sed/manual/sed.html

Topics

#sed#in-place editing#text processing#regex substitution

Community Discussion

No community discussion yet for this question.

Full XK0-004 Practice