Sed: can't solve this regex

I am trying to replace two lines in a php file using two sed commands , I can not find where I am wrong.

Want to convert from strings

setlocale(LC_ALL,  $_COOKIE['lang']);

and

putenv("LANGUAGE=".$_COOKIE['lang']);

for strings

setlocale(LC_ALL,  $_COOKIE['lang'].'.utf8');

and

putenv("LANGUAGE=".$_COOKIE['lang'].'.utf8');

so far i have come to the next but not working

sed -i "s/setlocale\(LC_ALL,  \$_COOKIE\['lang'\]\);.*$/setlocale\(LC_ALL,  \$_COOKIE\['lang'\]\.'\.utf-8'\)\;/" file.php

sed -i "s/putenv\('LANGUAGE='\.\$_COOKIE\['lang'\]\);.*$/putenv\('LANGUAGE='\.\$_COOKIE\['lang'\]\.'\.utf-8'\)\;/" file.php

I am definitely not an expert in sed and regex, so feel free to me?

+5
source share
2 answers

Try the following two:

sed 's/setlocale.LC_ALL,  ._COOKIE..lang...;/setlocale\(LC_ALL,  $_COOKIE\['\''lang'\''\].'\''.utf8'\''\);/g' file.php
sed 's/putenv..LANGUAGE...._COOKIE..lang...;/putenv\("LANGUAGE=".$_COOKIE\['\''lang'\''].'\''.utf8'\'');/g' file.php
+4
source

You should not avoid parentheses. There is no need to leave the corresponding characters in the replaced part:

sed "s/setlocale(LC_ALL,  \$_COOKIE\['lang'\]);.*$/setlocale(LC_ALL,  \$_COOKIE['lang'].'.utf-8')\;/"

The string putenvcontains double quotes, but your expressions look for single quotes. Therefore, it cannot match.

+3
source

All Articles