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

Re: [MacPerl] New user's first question on replacing html code



>I want to replace some html code in a whole bunch of files that are located in nested folders (all within a root folder). The replacement of the HTML code in the files is actually three different replacement (where I replace the header, footer, and some more code inside the file).
>
>As someone who is about to start using Perl. How difficult is this to do? I want to compare this to Python which I will be evaluating VS Perl. I will doing the replacement for about some 10,000 files, so should efficiency be a consideration?
>
>Thanks,
>Bill


I've been meaning to post this, it just keeps slipping my mind. I think it'll
work on Macs... I'm sure someone will post any necessary fixes, if it doesn't
work. NOTE: you may need to do some finagling, if your search/replaces each
involve multiple lines. NOTE2: each search/replace from the command line is
performed on each line of the file, thus, command line order is significant:
later search/replaces operate on already search/replaced lines.

-=-=-=-=-=-=-=-=-=-=-=- batchReplace -=-=-=-=-=-=-=-=-=-=-=-
#! /bin/perl
# Copyright 1998, Thomas R. Kimpton
# All rights reserved.

use File::Find;

#	The substitutions are done kind of bare:
#
#	batchReplace -s '/pattern/replacement/' -d .
# or
#	batchReplace -s '@pattern@replacement@' -d /tmp/goober -d /usr/var/messages
# etc.
#	Use '-b' to keep backups of files, if desired.
#
#	Insert your favorite 'It's-Not-My-Fault-If-You-Trash-Everything-In-The-Known-Universe'
#	disclaimer here -><-.

sub doBatchReplace
{
my $theFile = $_;

	open (INPUT,"<$theFile");
	open (OUTPUT,">$theFile.goober");
	while(defined($_ = <INPUT>))
	{
		$i = 0;
		for ($i=0;$i<$numExpr;$i++)
		{
			eval $expr[$i];
		}
		print OUTPUT;
	}
	close INPUT;
	close OUTPUT;
	rename ("$theFile", "$theFile.$$");
	rename ("$theFile.goober", "$theFile");
	if($keepBackup)
	{
		rename ("$theFile.$$", "$theFile.bak");
	}
	else
	{
		unlink ("$theFile.$$");
	}

	$_ = $theFile;
}


$keepBackup = 0;

$numExpr = 0;
$numDirs = 0;

while(defined($arg = shift(@ARGV)))
{
	if($arg eq "-s")
	{
		# make it a substitute command, add the global flag
		push(@expr, "s".shift(@ARGV)."g");
		$numExpr++;
	}
	elsif($arg eq "-d")
	{
		push(@dirs, shift(@ARGV));
		$numDirs++;
	}
	elsif($arg eq "-b")
	{
		$keepBackup = 1;
	}
	else
	{
		die "Didn't recognize '$arg'.\nUsage:\n$0 [-b] [-s substitution]+ [-d directory]+\n";
	}
}

if($numExpr == 0)
{
	die "You need to have at least one substitution\n";
}

if($numDirs == 0)
{
	die "You need to have at least one directory\n";
}

while (defined($theDir = shift(@dirs)))
{
	find(\&doBatchReplace,$theDir);
}


-=-=-=-=-=-=-=-=-=-=-=- batchReplace -=-=-=-=-=-=-=-=-=-=-=-

Oz:  So, do you guys steal weapons from the Army a lot?
Willow:  Well, we don't have cable, so we have to make
our own fun.
-- Innocence - Buffy the Vampire Slayer

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org