Multiple Edits with sed – How to Make Multiple Edits with a Single Call to sed

sed

I'm trying to use sed to edit a config file. There are a few lines I'd like to change. I know that under Linux sed -i allows for in place edits but it requires you save to a backup file. However I would like to avoid having multiple backup files and make all my in place changes at once.

Is there a way to do so with sed -i or is there a better alternative?

Best Answer

You can tell sed to carry out multiple operations by just repeating -e (or -f if your script is in a file).

sed -i -e 's/a/b/g' -e 's/b/d/g' file makes both changes in the single file named file, in-place. Without a backup file.

sed -ibak -e 's/a/b/g' -e 's/b/d/g' file makes both changes in the single file named file, in-place. With a single backup file named filebak.

Related Question