Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 14 Feb 2011 15:59:44 -0700
From:      Jarrod Slick <jarrod.sl@gmail.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: script help
Message-ID:  <4D59B3E0.1040403@gmail.com>
In-Reply-To: <3.0.1.32.20110214163437.019167e0@sage-american.com>
References:  <3.0.1.32.20110214163437.019167e0@sage-american.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe@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);
     }
}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4D59B3E0.1040403>