Niki Romagnoli
Niki Romagnoli

Reputation: 1506

sed: cannot solve this regular expression

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?

Upvotes: 5

Views: 113

Answers (2)

Avio
Avio

Reputation: 2708

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

Upvotes: 4

choroba
choroba

Reputation: 241848

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.

Upvotes: 3

Related Questions