Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed: cannot solve this regular expression

Tags:

regex

linux

sed

I'm trying to replace two strings in a php file using two sed commands, can't find where I'm wrong.

Want to transform from strings

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

and

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

to the strings

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

and

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

so far I've come to the following but does not work

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'm definitely not an expert in sed and regular expression, so go easy on me ok?

like image 520
TechNyquist Avatar asked Sep 05 '12 08:09

TechNyquist


2 Answers

Try these 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
like image 159
Avio Avatar answered Sep 25 '22 08:09

Avio


You should not escape the parentheses. There is no need to escape matching characters in the replacement part, either:

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

The putenv line contains double quotes, but your expressions searches for single quotes. Therefore, it cannot match.

like image 37
choroba Avatar answered Sep 24 '22 08:09

choroba