From owner-freebsd-questions@FreeBSD.ORG Wed Jul 23 13:20:17 2003 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 8340E37B401 for ; Wed, 23 Jul 2003 13:20:17 -0700 (PDT) Received: from webserver.get-linux.org (adsl-64-161-78-226.dsl.lsan03.pacbell.net [64.161.78.226]) by mx1.FreeBSD.org (Postfix) with SMTP id CB8C843FA3 for ; Wed, 23 Jul 2003 13:20:16 -0700 (PDT) (envelope-from oremanj@webserver.get-linux.org) Received: (qmail 1933 invoked by uid 1000); 23 Jul 2003 20:20:40 -0000 Date: Wed, 23 Jul 2003 13:20:40 -0700 From: oremanj@get-linux.org To: "Scott I. Remick" Message-ID: <20030723202040.GC1504@webserver> References: <1gf6uzxb9h9ir.1okxodxnyrkv2.dlg@40tude.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1gf6uzxb9h9ir.1okxodxnyrkv2.dlg@40tude.net> User-Agent: Mutt/1.4.1i cc: questions@freebsd.org Subject: Re: search & replace on multiple files X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Jul 2003 20:20:17 -0000 On Wed, Jul 23, 2003 at 10:56:19AM -0400 or thereabouts, Scott I. Remick wrote: > Hello... I'm trying to figure out a way to pull off the following: > > I have a subdir with many different sorts of text files (some nested in > additional subdirs, so recursion would be necessary) that need to have a > search & replace done on them. What's a quick way to script a global search > & replace on many/all text files in nested subirs? Hard way (tested): #!/usr/bin/env perl use File::Find; find sub { -f $_ or return; my $file = $_; my $filebak = "${file}~"; rename $file, $filebak or die "Can't rename $file to $filebak: $!\n"; open FILEBAK, $filebak or die "Can't open $filebak: $!\n"; open FILE, ">$file" or die "Can't write to $file: $!\n"; while () { # Replace OLD with the "search for" -- a regexp # Replace NEW with the "replace with" s/OLD/NEW/g; print FILE $_; } }, "."; # end replace.pl Easy way (not tested): find . -type f | perl -pi~ -e 's/OLD/NEW/g;' -- Josh > > Thanks! > > _______________________________________________ > 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"