From owner-freebsd-questions@FreeBSD.ORG Mon Feb 14 22:59:57 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 8A42A106564A for ; Mon, 14 Feb 2011 22:59:57 +0000 (UTC) (envelope-from jarrod.sl@gmail.com) Received: from mail-pz0-f54.google.com (mail-pz0-f54.google.com [209.85.210.54]) by mx1.freebsd.org (Postfix) with ESMTP id 58C6A8FC12 for ; Mon, 14 Feb 2011 22:59:57 +0000 (UTC) Received: by pzk32 with SMTP id 32so1022556pzk.13 for ; Mon, 14 Feb 2011 14:59:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=s/+ft32KR/bqB/DZrOm9Nbr835EIkqVBdX/rABOHBpI=; b=Zv+eVQRVvEk+QUTw44aYAmlj+Zdj1D2V8FVRCIRj8oilnJo8X1gTtpUz4r4kgRmhCd EbT3d0nYenDLStx2xsJuEgoP4uPdw8gLkFuvfbaHuVAihreejPCyFfq5h7qBkx7Us05q dmYF1a3+4oL8UYTg9CN/W9FKt5V5UQiM09H+0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; b=pCbGuNJImcvFFAo6fUY706sr/c6Fi0V2+Ei3KUWJ4s1pbUcOLdQRK8sTuEYZN2DK/J yYGUUjA76+Xnq9FPEx6iJP1BcZ4uxIGCAFABC8QfVfsLmJx5LUh66XvmSCVW3eAT2O2C JlneVR1iU/1hnjdQn1Gig6Vd/A8m0I/c3oKmM= Received: by 10.142.127.21 with SMTP id z21mr3597537wfc.238.1297724395729; Mon, 14 Feb 2011 14:59:55 -0800 (PST) Received: from jarrod-slicks-macbook.local ([206.123.101.89]) by mx.google.com with ESMTPS id w22sm4787107wfd.7.2011.02.14.14.59.53 (version=SSLv3 cipher=OTHER); Mon, 14 Feb 2011 14:59:54 -0800 (PST) Message-ID: <4D59B3E0.1040403@gmail.com> Date: Mon, 14 Feb 2011 15:59:44 -0700 From: Jarrod Slick User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.13) Gecko/20101207 Thunderbird/3.1.7 MIME-Version: 1.0 To: freebsd-questions@freebsd.org References: <3.0.1.32.20110214163437.019167e0@sage-american.com> In-Reply-To: <3.0.1.32.20110214163437.019167e0@sage-american.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: script help X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Feb 2011 22:59:57 -0000 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); } }