This is a bare-bones search and replace script that uses perls in-place editor and the File::Find module to get the job done quickly. It will search in all sub directories of the start directory and find and replace the text you specify in the type of file you specify. Keep in mind, this is a bare-bones implementation that is more for learning than for pratical use. It could be altered to accept command line arguments, input from the keyboard, or run as a CGI. That is up to you to figure out. :cheesy:
Search and Replace Script
#!/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);
print "Finished";
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.