Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed single quotes

Tags:

bash

escaping

sed

I've been banging into escaping single quote's problem using SED (Bash shell).

I need to make

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

into

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

What I've tried is:

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

Which ends up really jumbling the line.

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

I've tried the '\'' to escape single quotes and I think everything else under the sun but just can't get it quite there.

can anyone point to my probable obvious mistake?

Thank you.

like image 708
Chasester Avatar asked Dec 02 '22 00:12

Chasester


2 Answers

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

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, e.g. put the whole string to "s/../" may not be necessary. However, I just want to show how the \x27 worked.

like image 96
Kent Avatar answered Dec 20 '22 08:12

Kent


$ sed -i "s/\$cfg\['Servers'\]\[\$i\]\['password'\] = '';/\$cfg['Servers'][\$i]['password'] = '\$rootpassword';/g" file
like image 25
jcollado Avatar answered Dec 20 '22 08:12

jcollado