Welcome to WebmasterWorld Guest from 54.221.87.97

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Search and replace

script found here in old WebmasterWorld thread

     

beachlover

1:27 pm on Jan 13, 2007 (gmt 0)

10+ Year Member



Hi,

searched for a quick and dirty "Search and replace" script and found this great one here in a very old thread:

#!/usr/bin/perl
#the path to the files
$serverpath = "/path/to/files";#no trailing slash
#the docs you want changed
$doctype = "txt";
#the string to look for
$fine = "source string";
#what you want to replace it with
$replace = "replacement string";
#
#
#ok, getting started
print "Finding \"$fine\" and replacing it with \"$replace\"\n";
#going to directory
opendir(DIR, "$serverpath");
#looking for files
@files = grep(/\.$doctype$/,readdir(DIR));
closedir(DIR);
#lining the files up
foreach $file(@files){
$pa_fi = "$serverpath/$file";
#opening the file to read it
print "opening file $pa_fi\n";
open(FILE,"$pa_fi");
my(@subm) = <FILE>;
close(FILE);
#backup the document
rename $pa_fi, $pa_fi . '.bac';
print "making backup\n";
$n=0;
#printing new
foreach $line (@subm) {
$line =~ s/$fine/$replace/g;
open(SUB,">>$pa_fi");
print SUB $line;
close(SUB);
$n++;
}
print "$n lines searched in $file\n";
}

(found here: [webmasterworld.com...]

The only thing I would like to add is that all specified files will be changed (not just in /path/to/files but also in /path/to/files/foo and /path/to/files/foo/more-foo).

Any ideas? Thanks for the input!

perl_diver

8:08 pm on Jan 13, 2007 (gmt 0)

5+ Year Member




The only thing I would like to add is that all specified files will be changed (not just in /path/to/files but also in /path/to/files/foo and /path/to/files/foo/more-foo).

are you saying you want to also search through sub directories of the start directory for the files you want to open and replace some text in?

beachlover

9:12 pm on Jan 13, 2007 (gmt 0)

10+ Year Member



Exactly, that would fit my needs just perfectly.

perl_diver

11:33 pm on Jan 13, 2007 (gmt 0)

5+ Year Member



something like this should be considerably better:



#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
my $startdir = '/path/to/start/dir';
my $find = 'this';
my $replace = 'that';
my $doctype = 'txt';
print qq~Finding "$find" and replacing it with "$replace"\n~;
find(
sub{
return unless (/\.$doctype$/i);
local @ARGV = $_;
local $^I = '.bac';
while( <> ){
if( s/$find/$replace/ig ) {
print;
}
else {
print;
}
}
}, $startdir);

If you run this as a CGI you may need to add appropriate http headers. If you don't want to retain the backup files (.bac) you can unlink them during the process.

Make sure to test first in a test directory with some test files.

beachlover

11:02 am on Jan 14, 2007 (gmt 0)

10+ Year Member



perl_diver, thanks a lot for your help! Your script works just perfectly.

Can I just uncomment the line

#local $^I = '.bac';

to deactivate the creation of bac-files?

perl_diver

7:08 pm on Jan 14, 2007 (gmt 0)

5+ Year Member



you can try. If you're using linux or unix I think you don't need it, but windows might throw an error if you don't use it, something like:

can't do in place editing blah blah blah....

you can also try setting it to an empty string:

local $^I = '';

beachlover

8:22 pm on Jan 14, 2007 (gmt 0)

10+ Year Member



Well, what can I say:

>you can also try setting it to an empty string:
>local $^I = '';

just did the magic!

Once again a very BIG THX for your help!

perl_diver

4:59 am on Jan 15, 2007 (gmt 0)

5+ Year Member



You're welcome.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month