homepage Welcome to WebmasterWorld Guest from 54.225.1.70
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Search and replace
script found here in old WebmasterWorld thread
beachlover




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

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




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


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




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

Exactly, that would fit my needs just perfectly.

perl_diver




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

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




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

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




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

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




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

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




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

You're welcome.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved