How can backup and then modify a file with sed?


I need to overwrite some configs on many servers at once and I want to modify the origianl config file while creating a backup, can all that be done with sed, just for curiosity?

Someone was mentioning this pattern, but it doesn't work.

new_port="..."
sed -i .bak "s/port=2334/${new_port}/g"
---------------Answer---------------
sed -i .bak "s/port=2334/${new_port}/g"

is nearly what you’re looking for; the fix is to remove the space after -i:

sed -i.bak "s/port=2334/${new_port}/g"

If new_port only has the port number, you’ll also have to repeat port=:

sed -i.bak "s/port=2334/port=${new_port}/g"

All of this only works if you actually specify the names of the files to be processed, after the sed expression.


Previous : Can't resize Dash Icons in Settings (No option)
Next : Linux script to see what packages were not downloaded from a folder