Replace all instances of a string in a file
by iicapn from LinuxQuestions.org on (#5E4C0)
I'm trying to script a hostname change... for instance in my /etc/hosts file, I have a line that says:
Code:127.0.1.1 debian10.mydomain.com debian10If I run the following on the command line, it shows me what I want:
Code:sed 's|debian10|mybox|g' /etc/hostsBut it doesn't replace my /etc/hosts file... it just shows me the changes.
adding ` > /etc/hosts` to the end doesn't seem to help. Is this the best way to replace a string in a file?
Many thanks in advance :-)


Code:127.0.1.1 debian10.mydomain.com debian10If I run the following on the command line, it shows me what I want:
Code:sed 's|debian10|mybox|g' /etc/hostsBut it doesn't replace my /etc/hosts file... it just shows me the changes.
adding ` > /etc/hosts` to the end doesn't seem to help. Is this the best way to replace a string in a file?
Many thanks in advance :-)