Setting config database user and password using sed


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Setting config database user and password using sed
# 1  
Old 05-13-2019
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:

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

need to find:
Code:
define("__DATABASE_PASSWORD__",              "oldpass");

and to be replaced by:
Code:
define("__DATABASE_PASSWORD__",              "newpass");

Do you have any idea ?

Thank you for your help.
# 2  
Old 05-13-2019
Maybe just keep it simple?

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

In your case:

Code:
sed -i.bak  's/oldpass/newpass/g' config.php

This User Gave Thanks to Neo For This Post:
# 3  
Old 05-13-2019
Code:
sed 's%\(define("__DATABASE_PASSWORD__",\s*"\)\([^"]\+\)%\1newpass%g'

I guess that newpass will look like /path/to/dir/file (I was mistaken) than
Code:
sed 's/\(define("__DATABASE_PASSWORD__",\s*"\)\([^"]\+\)/\1newpass/g'


Last edited by nezabudka; 05-13-2019 at 05:51 PM..
# 4  
Old 05-13-2019
Hello,
many thanks for your reply.

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

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

Because I have several settings with the same string ... Smilie

Example:
Code:
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

Code:
define("__DATABASE_PASSWORD__",              "oldpass");

by

Code:
define("__DATABASE_PASSWORD__",              "newpass");

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

output:
Code:
define("__DATABASE_USER__",              "oldpass");
define("__DATABASE_PASSWORD__",              "oldpass");

Good evening.
# 5  
Old 05-13-2019
Try this one:

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

# 6  
Old 05-14-2019
Dear stomp,
Thank you very much for your help, it's works very well !!!
# 7  
Old 05-14-2019
Making it a bit more precise, so it would not match e.g. a __DATABASE_PASSWORD_ENABLED__
Code:
sed '/"__DATABASE_PASSWORD__"/ s/oldpass/newpass/'

The alternative is without a selector, instead have the substitution match include the selector:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

DB Password encryption in config file

Hi Gurus, I need to encrypt the Db passwords which are stored in a configuration file (.txt) as below: stage_db_pwd=ABC this is test line content_db_pwd=123def This is test line 2 stg_db_name=xyz I want to encrypt all the password fields (identified by "pwd"), encrypt them in the same... (3 Replies)
Discussion started by: ashishpanchal85
3 Replies

2. Shell Programming and Scripting

Setting up postgreSql database

Hi I have a small bash script which I want to run on an Amazon EC2 Ubuntu instance for setting up a postgreSQL database: #!/bin/bash USERNAME='postgres' start=$SECONDS TMP_DIR=/local/test/4g4d PORT=23456 rm -rf $TMP_DIR/db mkdir -p $TMP_DIR/ echo "creating database..."... (3 Replies)
Discussion started by: Helveticus
3 Replies

3. Solaris

Is there a difference between setting a user as nologin and setting it as a role?

Trying to figure out the best method of security for oracle user accounts. In Solaris 10 they are set as regular users but have nologin set forcing the dev's to login as themselves and then su to the oracle users. In Solaris11 we have the option of making it a role because RBAC is enabled but... (1 Reply)
Discussion started by: os2mac
1 Replies

4. UNIX for Advanced & Expert Users

Can we Automate the User creation and setting password through a script in solaris 10

Hi, I am using Solaris 10 OS and Bash shell.Is there any way can we automate User creation and setting passwords through a script or any freeware tool. Advance thanks for your response. (1 Reply)
Discussion started by: muraliinfy04
1 Replies

5. UNIX for Dummies Questions & Answers

Firefox: Setting about:config options upon install

Is there a way to set the about:config options for Firefox automatically as part of the installation of Firefox? (3 Replies)
Discussion started by: figaro
3 Replies

6. Solaris

Password Setting

Hi: Could I set the: - Login Time-out Interval - Password History Count - Lockout Duration - Lockout Threshold for user account in Sun Solaris 5.8. Thanks for your help (6 Replies)
Discussion started by: mlsun
6 Replies

7. UNIX for Advanced & Expert Users

setting password for user using useradd?

hi all i am writing a script to create user and group from the input given to script for eg. script needs to values 1. mode - 1 or 2 2. id - if mode is 1 then id should be 2 char like x1 / v1 / v2 if mode is 2 then id should be 1 char like x / v / e from these to values group is... (1 Reply)
Discussion started by: zedex
1 Replies

8. UNIX for Advanced & Expert Users

UX Kernel setting while database installation

Hi All I am kinda wondering about the UX kernel parameters, how we configure and why do we configure ???.. especially the 'semaphores'. Could somebody throw some light on this and would really appreciate it. (1 Reply)
Discussion started by: Prince_Charming
1 Replies

9. UNIX for Advanced & Expert Users

I can't fire up MySql on my Fedora Server - Obvious config setting?

Hiya All, I can't fire up MySql on my FC-3 Server at work. I get the Error message at the end of this post. Looks like some Config problem on the Server itself. (Ie web server and MYSQL is all on the same box. I get the same errror when trying to fire up MYSQL at the Box, as well as... (2 Replies)
Discussion started by: marty 600
2 Replies
Login or Register to Ask a Question