Patrickkasie
Patrickkasie

Reputation: 35

Changing a line of text with sed with special characters

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?

Upvotes: 0

Views: 222

Answers (3)

Ed Morton
Ed Morton

Reputation: 203684

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:

$ awk -v str="\$cfg['Servers'][\$i]['AllowRoot']" 'index($0,str){sub(/false/,"true")} 1' file
    //some text here
    $cfg['Servers'][$i]['AllowRoot'] = true;<br />
    //some more text here
 Run code snippetHide resultsExpand snippet

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.

Upvotes: 0

chepner
chepner

Reputation: 531430

The [ and ] need to be escaped to match literal brackets, instead of inadvertently starting a bracket expression. This should work:

$ sed -i "/\$cfg\['Servers'\]\[\$i\]\['AllowRoot'\]/s/false/true/"  /var/www/html/phpMyAdmin/config.inc.php

Upvotes: 2

toftis
toftis

Reputation: 1080

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:

sed -i "s#$cfg['Servers'][$i]['AllowRoot'] = false;<br />#$cfg['Servers'][$i]['AllowRoot'] = true;<br />#g" input.txt

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:

cat allow_root_true.sed
s#['Servers'][]['AllowRoot'] = false;<br />#['Servers'][]['AllowRoot'] = true;<br />#g

and run the command using sed -f like this:

sed -i -f allow_root_true.sed input.txt

Warning -i will change the input file

Upvotes: 0

Related Questions