francoisromain francoisromain - 24 days ago 9
Linux Question

modify a file using a bash script with sed

similar to these questions:



In a file like this:

define('_DB_NAME_', 'anything');
define('_DB_USER_', 'something else');
define('_DB_PASSWD_', 'and another value');


how to use sed to replace the value
anything
,
something
and
another
(which are unknown) by refering to the key values
_DB_NAME_
,
_DB_USER_
and
_DB_PASSWD
?

Answer
sed "s/\(define('_DB_NAME_', \).*/\1'new value');/" filename

and likewise for the other two.

If you want to alter the file in place, you can use -i, but you have to be careful, different versions of sed handle that differently.