t Book t Book - 1 year ago 104
Perl Question

Perl search file recursive for string and replace

in terminal with perl how can I search all php files starting recursive from current working directory for a single or multiline pattern like:

<script>var a=''; * hamoorabi.com * </script>

Read like: find all between
<script>var a='';
but only if contains
and replace it with an empty string (remove it).

As it´s javascript code there can be a bunch of unescaped characters inside the search string.

Answer Source

From a unix or cwygin prompt:

$ find . | grep .php | xargs ./xx1.pl

Where perl script xx1.pl is :


use strict;
use warnings;

undef $/;
for (@ARGV) {
        my $content = <FILE>;
        $content =~ s/(<script>.*?)(hamoorabi.com)(.*?<\/script>)/$1$3/sg;
        print FILE $content;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download