View Full Version : Editing a file using sed/ed

May 17th, 2009, 07:03 AM

I am new to shell scripting and I need to edit a few files in a script as part of an automation process. I found out that its possible using sed/ed but the examples are a bit confusing to me and I dont want to risk testing it as it might break the system.
Suppose I want to add the following to the file " /etc/network/interfaces" :

auto eth1
iface eth1 inet static

How would I do it in a script using sed/ed assuming that the file was empty initially. File can be created using "touch" or using sed/ed.


May 17th, 2009, 09:34 AM
If the file is empty (and you're sure about it!), you can simply use echo to put your text in the file:

echo "auto eth1
iface eth1 inet static" > filename

If the file isn't empty, and you want to add the text to the end, you can use:

echo "auto eth1
iface eth1 inet static" >> filename


May 17th, 2009, 06:03 PM
Thanks a lot Steve. This seems to work for me.