Setting config database user and password using sed

Hello everybody,

I need to modify 200 files using a patern matching, I would like to do it with sed but it's not working with the following syntax:

sed -e 's/DATABASE_PASSWORD.*oldpass/DATABASE_PASSWORD__', 'newpass/g' config.php

need to find:

define("__DATABASE_PASSWORD__",              "oldpass");

and to be replaced by:

define("__DATABASE_PASSWORD__",              "newpass");

Do you have any idea ?

Thank you for your help.

Maybe just keep it simple?

sed -i.bak 's/old-text/new-text/g' filename

In your case:

sed -i.bak  's/oldpass/newpass/g' config.php
1 Like
sed 's%\(define("__DATABASE_PASSWORD__",\s*"\)\([^"]\+\)%\1newpass%g'

I guess that newpass will look like /path/to/dir/file (I was mistaken) than

sed 's/\(define("__DATABASE_PASSWORD__",\s*"\)\([^"]\+\)/\1newpass/g'

Hello,
many thanks for your reply.

@Neo, Unfortunately I can't use this command:

 sed -i.bak  's/oldpass/newpass/g'

Because I have several settings with the same string ... :frowning:

Example:

define("__DATABASE_USER__",              "oldpass");
define("__DATABASE_PASSWORD__",              "oldpass");

I would like to change only the DATABASE_PASSWORD and do not change the string for DATABASE_USER.

I try to change the following bloc using sed substitution: define("__DATABASE_USER__", "oldpass");

@nezabudka, thank you for your help but your command line do not replace

define("__DATABASE_PASSWORD__",              "oldpass");

by

define("__DATABASE_PASSWORD__",              "newpass");
shell command: sed 's/\(define("__DATABASE_PASSWORD__",\s*"\)\([^"]\+\)/\1newpass/g' config.php

output:

define("__DATABASE_USER__",              "oldpass");
define("__DATABASE_PASSWORD__",              "oldpass"); 

Good evening.

Try this one:

sed -i.bak '/DATABASE_PASSWORD/s/oldpass/newpass/'

Dear stomp,
Thank you very much for your help, it's works very well !!!

Making it a bit more precise, so it would not match e.g. a __DATABASE_PASSWORD_ENABLED__

sed '/"__DATABASE_PASSWORD__"/ s/oldpass/newpass/'

The alternative is without a selector, instead have the substitution match include the selector:

sed 's/\("__DATABASE_PASSWORD__".*\)oldpass/\1newpass/'

Everything that matches is substituted, so one must capture in a \(group\) and give it back as \1 in the substitution string.