XK0-005 · 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…
The correct answer is C. sed -i 's|.50|.100|' serverlist.txt. To update IP addresses in place within the serverlist.txt file, the sed command with the -i option performs in-place editing, and s/.50/.100/ replaces the first occurrence of .50 with .100 on each line.
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
(22 responses)- A5% (1)
- B18% (4)
- C73% (16)
- D5% (1)
Why each option
To update IP addresses in place within the `serverlist.txt` file, the `sed` command with the `-i` option performs in-place editing, and `s/.50/.100/` replaces the first occurrence of `.50` with `.100` on each line.
This command would print the modified content to standard output but would not save the changes back to the `serverlist.txt` file because it lacks the `-i` option for in-place editing.
The `sed` command does not have a `-g` option for global replacement; instead, the `g` flag is placed at the end of the substitution command (e.g., `s/old/new/g`), and the `a` flag is not a standard substitution flag.
The `sed` command with the `-i` option modifies the file in place, and the substitution command `s/.50/.100/` finds the first instance of ".50" on each line and replaces it with ".100", correctly updating the IP addresses for the web servers.
While `sed -i 's|.50|.100|g'` would also accomplish the task, the `g` flag (global replacement) is redundant in this specific case because the target pattern ".50" appears only once per relevant line for the IP address.
Concept tested: sed in-place file editing
Source: https://man7.org/linux/man-pages/man1/sed.1.html
Topics
Community Discussion
No community discussion yet for this question.