0

I'm looking for a regexp to quickly replace the password inside the following wp-config file, like so:

define('DB_PASSWORD', 'xxxx');

Now the config file has many defines, so it essential to only replace the DB_PASSWORD parameter. I also don't know the old password, meaning i can't do a replace 'xxxx' with 'yyyy'.

I'm on Linux with SED.... (need to replace many files) so something like:

cat wp-config.php | sed 's/[here the regexp]/new password/'

Thanks!

Roger
  • 800
  • 1
  • 6
  • 17
  • For the downvoters. If you want to make point, explain what is wrong, so i can adjust the question.... – Roger Aug 25 '13 at 15:03

2 Answers2

3

It is tricky if you don't know the old password. But you might try something like this -

 sed -e "s;\(define([[:space:]]*'DB_PASSWORD',[[:space:]]*\)\(.*\)\()\;\);\1'NewPassHere'\3;g" wp-config.php

This will match the text before password, then password, then the remaining text after password. It will save it in \1,\2,\3 - at the end it replaces only \2 (which is the password).

Once you visually confirm it has replaced the passwords succesfully, you can use 'sed -i' to replace the passwords. Please take a backup of wp-config.php before you run this one liner.

Daniel t.
  • 9,291
  • 1
  • 33
  • 36
0

Just in case someone want it in Perl (most people nowadays don't know you have such tool by default in many distros). The oneliner replaces everything between second pair of single quotes with the new password.

perl -i -pe" s|^define.'DB_NAME', '(.*?)'.;|define('DB_PASSWORD', 'newpassword');|" wp-config.php

Here dots after "define" and before ";" means "(" and ")" respectively to avoid construction like "(" which makes the regex more readable. Also, for sake of readability of the replacement part, I did not wrap string parts which are before and after the password into variable, but of course it can be done.

If you run the oneliner in a script and need to get the new pass from a shell variable:

newpass=$(do_something_here)
perl -i -pe" s|^define.'DB_NAME', '(.*?)'.;|define('DB_PASSWORD', '$newpass');|" wp-config.php
Putnik
  • 2,217
  • 4
  • 27
  • 43