1

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"
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
user12145
  • 1,115
  • 6
  • 28
  • 47

2 Answers2

1

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' {} \+
Dan Carley
  • 25,617
  • 5
  • 53
  • 70
0

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"
Helvick
  • 20,019
  • 4
  • 38
  • 55