You are not logged in.
Hello!
I have the string that is for Drupal,
$ db_url = 'mysql :/ / username: password @ localhost / database name';
where I need to pick out the "password" and replace it with another. I have tested with both custom and cut without any steps forward. Anybody have any tips? I already have a random password generator and it works fine with ex WP and Joomla. But the string int Drupal is a bit more tricky.
Thank you in advance.
Edit: Tested with
echo 'mysql://username:password@localhost/databasename'|sed 's/@localhost/ /'| sed 's/username:/ /'|awk '{print $2}'
which writes out the "password" but i need to replace it aswell.
Last edited by Afnaf (2012-05-14 11:53:14)
Offline
I don't know what drupal is, but if you're willing to pass this string through a script first, I think most scripting languages can do this fairly trivially... although I don't know how to do it off-hand in bash. In python you might have:
db_url = 'mysql :/ / username: password @ localhost / database name'
then, turn the sentence into a list with
L = db_url.split(' ')
which makes each word in the string an element of the list L, where words are space-separated. The password will be the 4th (starting from zero) element of the list. Just to be careful, you could let Python figure out the index for you
pw_index = L.index('password')
Change it with
L[pw_index] = 'newpassword'
Then stitch the sentence back together with (be sure the get the tab right. There's a tab in that code)
new_db_url=L[0]
for i in range(1,len(L)):
new_db_url = new_db_url + L[i]
and print it with
print(new_db_url)
Not sure how much this helps, but maybe it'll give you some inspiration? You'd have to implement the split() function yourself in C, but it's only a few lines.
Last edited by Yurlungur (2012-05-14 08:53:39)
Lenovo Thinkpad T420; Intel sandy bridge i7 2.7GHz; integrated graphics card; 4GB RAM; wifi; Arch; Xmonad WM
Offline
Thank you for the tip I have come down to this solution myself:
echo 'mysql://username:password@localhost/databasename' | sed -e "s_:[^:]*_newpass_g"
But since there is two semicolons, it also replaces the first one, so can i say to sed just to replace after the second semicolon?
Edit: Maybe not :*(
Last edited by Afnaf (2012-05-14 09:01:21)
Offline
I went with this:
echo '$db_url = mysql://username:password@localhost/databasename'|sed 's/@localhost/ /'| sed 's/username:/ /'|awk '{print $4}'
Last edited by Afnaf (2012-05-14 09:46:21)
Offline
I realize this is almost a week late, but this replaces the password field (in this case with "PASSWOID"), uses only one instance of sed and doesn't spawn an unnecessary instance of echo or awk:
[corey@sariss ~]$ sed 's,\(.*mysql://[^:]\+:\)[^@]\+\(@.*\),\1PASSWOID\2,' <<< '$db_url = mysql://username:passwoid@localhost/databasename'
$db_url = mysql://username:PASSWOID@localhost/databasename
Last edited by cmtptr (2012-05-27 22:20:06)
Offline