I’m sure many are familiar with the general substitute method of
sed for replacing something in a file, i.e.
sed -i s/this/that/g ~/myfile but recently I wanted to replace an entire line in a file, using bash.
I wanted to modify the
localhost entry in
/etc/hosts to include the static hostname too, all tab delimited. This is how I did it.
sed -i /^127.0.0.1/c\127.0.0.1\\t`hostname -f`\\t`hostname`\\tlocalhost.localdomain\\tlocalhost /etc/hosts
Find the line that begins with
127.0.0.1 and replace it with itself, plus the result of the hostname commands, etc. This gives me a line like so:
127.0.0.1 pc.fqdn pc localhost.localdomain localhost
I’m sure someone else knows a much more efficient way of doing this, but oh well, it works