在网站文件中用一行替换多行:bash? 蟒蛇?

I am looking for a command line solution to replace a potentially dangerous string in php files by a safer one, on a Linux server. I'd like to conduct the replace in all hosted sites, and then have it run as a daily cronjob to be on the safer side. Bash or PHP would be perfect I guess, but I'm ok with Python if that's more efficient. I could find many examples but could not adapt them to my case, which is

  • scan all /httpdocs/ subdirectories in /home/vhosts/(sitename)/ but scanning all files in /home/vhosts will do too.

  • determine if there's a file containing a multiple-lines code, ie

$authsites = array ( 'flickr.com', 'picasa.com', 'blah.com', );

and if so replace it (sed ?) with

$authsites = array ();

and log where it replaced it.

Help greatly appreciated, thanks.

i use command line perl for bulk search and replace

perl -pi -e 's/FIND IT/REPLACE IT' /home/name/public_html/html/*.html 

How about this awk one-liner. You can use it with find or for loop to do mass substitution.

awk '/\$authsites/{sub(/\(.*\)/,"( )");print;next}1' INPUTFILE

Test:

[jaypal:~/Temp] cat file00
$authsites = array ( 'flickr.com', 'picasa.com', 'blah.com', );
$authsites = array ( 'flickr.com', 'aaaa.com', 'blah.com', );
fefe
efef
$authsites = array ( 'flickr.com', 'picasa.com', );

[jaypal:~/Temp] awk '/\$authsites/{sub(/\(.*\)/,"( )");print;next}1' file00
$authsites = array ( );
$authsites = array ( );
fefe
efef
$authsites = array ( );

A for loop like this could work (though there may be better ways)

You can change the name according to your files

for i in $(find . -type f -name "file*" -exec grep -H "\$authsites" {} \;| cut -d":" -f1 | uniq); do awk '/\$authsites/{sub(/\(.*\)/,"( )");print;next}1' $i > $i.new; done