script help
Jarrod Slick
jarrod.sl at gmail.com
Mon Feb 14 22:59:57 UTC 2011
On 2/14/11 3:34 PM, Jack L. Stone wrote:
> Hello folks:
>
> No doubt this will be easy for those with scritping abilities.
>
> I have a gazillion files by the same name and each contains the same line
> requiring the same change. But the problem is that they are in many
> different directories on a server with numerous domains. While I could
> handle the change using a single directory within my abilities, I'm unsure
> how to do a search and replace throughout the many domains and their
> directories. Don't want to mess up. Here's what I'm trying to do:
>
> # find all of the same filenames (copyright.htm) and then replace the year
> 2010 with 2011 in each file. Once I have a working script, I should be able
> to add it as a cron job to run on the first day of each new year.
>
> Any help appreciated.
>
> Thanks!
> Jack
>
> (^_^)
> Happy trails,
> Jack L. Stone
>
> System Admin
> Sage-american
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"
Something like this should work (*UNTESTED*):
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
my @directories = qw(/var/www/html /var/www/html2 /etc); #if you don't
know all the directories and are okay with the script running for a
while you could just specify /
my $line = quotemeta("Copyright 2010"); # Or whatever your line actually
is . . .
my $copyright_file = quotemeta("copyright.htm");
find(\&wanted, @directories);
sub wanted {
if($_ =~ $copyright_file) {
open(my $fh, '<', $File::Find::dir.'/'.$copyright_file) or die
"Couldn't create read handle: $!\n";
my $new_file = undef;
while(<$fh>) {
if($_ =~ /^$line$/) {
$_ =~ s/2010/2011/;
}
$new_file .= $_."\n";
}
close($fh);
open($fh, '>', $File::Find::dir.'/'.$copyright_file) or die
"Couldn't create write handle: $!\n";
print $fh $new_file;
close($fh);
}
}
More information about the freebsd-questions
mailing list