The name in the title says it all. However, I’m absolutely the worst with the sed command. So I’m trying to edit the following file:
/var/www/html/phpMyAdmin/config.inc.php
I want to edit the line that says
$cfg['Servers'][$i]['AllowRoot'] = false;
into the following
$cfg['Servers'][$i]['AllowRoot'] = true;
It has so many special characters and whatnot and I have no prior knowledge of how sed works. So here’s some commands I’ve tried to specifically edit that one line.
sed -i "/*.AllowRoot.*/$cfg['Servers'][$i]['AllowRoot'] = true;/" /var/www/html/phpMyAdmin/config.inc.php
sed -i "/*.AllowRoot.*/$cfg['Servers'][$i]['AllowRoot'] = true;/" /var/www/html/phpMyAdmin/config.inc.php
# this one finds the line successfully and prints it so I know it's got the right string:
sed -n '/AllowRoot/p' /var/www/html/phpMyAdmin/config.inc.php
sed -i "s/'AllowRoot|false'/'AllowRoot|true'/" /var/www/html/phpMyAdmin/config.inc.php
I have absolutely no idea what I’m doing and I’m not learning a whole lot besides the feeling that the last command splits up 'AllowRoot|false'
makes sure that both must be present in the sentence to come back as a result. So to my logic, I thought changing the word false
into true
would make that happen, but nothing. The other commands return… bizarre results at best, one even emptying the file. Or that’s one of the commands I had not written down here, I’ve lost track after 50 attempts. What is the solution here?
3
Answers
There is not many things to escape in sed. Main problem in your line is
/
which you have chosen as delimiter (most common, but not required). I suggest you use#
and the following will work:however you need to think about bash interpreter as well. $i and $cfg will be interpreted as variables. My suggestion is that when you want to match a string like this to put the sed expression in a text file like this:
and run the command using
sed -f
like this:Warning
-i
will change the input fileThe
[
and]
need to be escaped to match literal brackets, instead of inadvertently starting a bracket expression. This should work:sed can’t do literal string matching which is why you need to escape so many characters (see Is it possible to escape regex metacharacters reliably with sed), but awk can:
In the above we only have to escape the
$
s to protect them from the shell since the string is enclosed in"
s to allow it to include'
s.