trying to replace text patterns with dollar signs and ()s, can't get it to work. please help

find /var/www/vhosts/prod/xxx/  -name "*.php"|xargs perl -w -i -p -e "s/mysql_fetch_array\($res,MYSQL_ASSOC\)/mysql_fetch_assoc(\$res);/g"
link|improve this question

72% accept rate
feedback

2 Answers

up vote 1 down vote accepted

I suspect that your shell is expanding $res. Enclose the regex in single quotes to prevent this.

A slightly more efficient variation without xargs and perl:

find /var/www/vhosts/prod/xxx/ -name "*.php" -exec sed -i 's/mysql_fetch_array($res,MYSQL_ASSOC)/mysql_fetch_assoc($res);/g' {} \+
link|improve this answer
feedback

You also need to escape the $, ( and ) symbols in the search regex, you've already escaped the $ in the replacement string correctly.

With Perl 5.10.1 I have to use the -i.bak parameter to get this to work, otherwise the inplace edit switch complains that I don't have a backup.

find /var/www/vhosts/prod/xxx/ -name "*.php"|xargs perl -w -p -i.bak -e "s/mysql_fetch_array\(\$res,MYSQL_ASSOC\)/mysql_fetch_assoc(\$res);/g"
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.