A systems administrator needs to update an application's configuration file, web-app.conf. They must replace all occurrences of the server's old IP address, 172.16.10.50, with its new IP address, 10.100.30.20. The change must be applied directly to the file itself without manual intervention. Which of the following commands should the administrator use?
sed 's/172.16.10.50/10.100.30.20/g' web-app.conf
sed -i 's/172.16.10.50/10.100.30.20/g' web-app.conf
The correct command is sed -i 's/172.16.10.50/10.100.30.20/g' web-app.conf. The sed utility is the stream editor, and the -i option modifies the file in-place. The s/old/new/g expression searches for all (g) occurrences of the old IP address and replaces them with the new one. Simply running sed without -i would only print the changes to standard output. Using awk with standard redirection to the source file would truncate the file before it can be read. grep is a tool for searching and does not have a native replace function.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What are some common use cases for the 'sed' command?
Open an interactive chat with Bash
How does 'sed' differ from 'awk'?
Open an interactive chat with Bash
What does the 'g' flag do in 'sed's search-and-replace command?