Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"
share|improve this question
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' {} \+
share|improve this answer

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"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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