mjn wrote:
> I am looking to walk a directory structure, look at all the files ending
> in .html, and examine them for a bit of text and replace that text if its
> found.
>
> Awhile ago someone was kind enough to answer me for doing something like
> this if all of the files are in a certain directory but I haven't found a
> way to do this recursively.
>
> I tried this:
>
> perl -pi.bak -e 's/bob/dick/' testing/.
>
> But that doesn't seem to work. If this command would work like I
> think it should and it could be made only to affect a certain file
> extension, that would rule.
>
> So is there something I am missing or should I start writing something a
> little bigger for the job?
>
I'm sure there *is* a way to write a one-liner to do this job, but I'm
not convinced that you
should attempt it. To recurse into directories, I use something like:
sub checkdirs(){ #sorry, I don't know argument prototyping
my $dir = $_[0]; #first argument passed in
opendir(DH, "$dir") or die "$0: failed to open handle for $dir: $!\n";
my @listing = readdir(DH);
closedir(DH);
foreach my $file (@listing){
if((!-d $file) && ($file ~= /*\.html^/){
open(FH, "$file") or die $0: failed to open $file for read/write: $!\n";
while(<FH>){
$_ ~= s/dick/bob/g;
}
close(FH);
} elsif ((!-d $file) || ($file ~= /$\.^/) || ($file ~= /$\.\.^/)) {
#optional debugging output would go here
} else {
checkdirs($file);
}
}
I apologize for problems with indentation; Mozilla isn't being very
helpful. :)
You might want some sort of debugging in the 'elsif' clause; the only
things that
should fall through to it are '.', '..', and anything that doesn't end
in '.html', but isn't a directory.
Hope that's helpful to you!
--
<---------------------------------------------------------------------->
Chris H. Bidler cbidler at talkware.net
Random Task Handler, Sysadmin Group
Universal Talkware Corp.
"In any event, is a O^(log N) search that returns the wrong answer
really better than an O^N search that returns the right answer?"
<---------------------------------------------------------------------->