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.
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)- A3% (1)
- B7% (2)
- C83% (24)
- D7% (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.
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.
'-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.
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.
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
Community Discussion
No community discussion yet for this question.