[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Search and replace in many files.



Anders Gardebring skrev/wrote:
>I wonder if someone could help me with a problem I got.
>I need to search and replace the occurance of a specific word to another
>(for example "word1" => "word2") in all files matching a criteria (for example
>*.html) in a given path including all paths below the top one.
>Does anyone know of a cgi-script that can do this, or does anyone have any
>ideas on how this could be done?
>

A not-so-elegant but at least working example (without any error checking :-(
(it takes a little more to make it recursive)
########
chdir "disk:my folder:";

while (<*.html>) {
	my $fil = $_;
	print STDOUT "$_\n";
	$tmp = "$fil.orig";
	open UT, ">$tmp";
	select UT;
	open INFIL, "<$fil";
	while (<INFIL>) {
		s/word1/word2/g;
		print UT;
	}
	close INFIL;
	close UT;
	unlink $fil;
	rename $tmp, $fil;
}
########

(if you want, we can discuss this in Swedish, but then we wouldn't get the mostly invaluable comments from this list...)

Christian
<http://www.solvare.se/individer/christian/>
________________________________________________________
Solvare - mathematics consulting & macintosh automation     
<mailto:info@solvare.se>         <http://www.solvare.se>       



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch