Tag Archives: sed

Sed off

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 /^\\\t`hostname -f`\\t`hostname`\\tlocalhost.localdomain\\tlocalhost /etc/hosts

Find the line that begins with and replace it with itself, plus the result of the hostname commands, etc. This gives me a line like so:   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 🙂