Single quotes

I ran into a single quote problem using SED (Bash shell).

I need to do

$cfg['Servers'][$i]['password'] = ''; 

in

 $cfg['Servers'][$i]['password'] = 'mypassword'; 

I tried:

 sed -i "s/$cfg['Servers'][$i]['password'] = '';/$cfg['Servers'][$i]['password'] = '$rootpassword';/g" /usr/share/phpmyadmin/libraries/config.default.bak 

Which ends by really fingering the line.

 $cfg['Servers'][$i]['password['Servers'][]['passsword'] = 'mypassword' 

I tried "\" to avoid single quotes, and I think that everything else is under the sun, but just can't get it at all.

Can anyone point out my likely obvious mistake?

Thanks.

+4
source share
3 answers
 $ sed -i "s/\$cfg\['Servers'\]\[\$i\]\['password'\] = '';/\$cfg['Servers'][\$i]['password'] = '\$rootpassword';/g" file 
+4
source

instead of escaping, you can use \x27 for a single quote. this works not only for sed, for awk, etc. Also.

see test:

 kent$ cat tt $cfg['Servers'][$i]['password'] = '' kent$ sed -r 's/(\$cfg\[\x27Servers\x27\]\[\$i\]\[\x27password\x27\] \= \x27)\x27/\1mypassword\x27/g' tt $cfg['Servers'][$i]['password'] = 'mypassword' 

note that the sed line may not be the best solution for your problem, for example. putting the whole line on "s /../" may not be necessary. However, I just want to show how \ x27 works.

+13
source

Try the following:

 sed -i "/Servers.*password.*''$/s/''/'foo'/" /path/to/your/testfile 

Match the line containing "anything ..." .... "Anything ...". (end with '' ), and in this line replace '' with 'foo'

This may match multiple lines, but only the first event will be changed. Check it, most likely that .. Servers .. password .. '' is on only one line.

Or you can just avoid everything.

+1
source

Source: https://habr.com/ru/post/1386390/


All Articles