#!/usr/bin/perl -w
# Script to make text replacements to a list of files
#
# The author of this script makes no warranty,
# express or implied, that this script will
# do anything useful at all, and any use
# of it is entirely at your own risk.
#
# by Lev Koszegi 4/10/2006
use strict;
use File::Copy;
use File::Basename;
use Cwd;
usage() unless $#ARGV == 1; #Require two arguments (list file & patter
+n file)
my (@files, @patterns);
my ($pattern, $filename);
my $now = time;
my $dir = cwd;
open FILE_LIST, $ARGV[0]
or die "Cannot open file list: $!";
chomp(@files = <FILE_LIST>);
#Test the lines of FILE_LIST to make sure files exist;
#if not, note which one is bad and die.
foreach $filename (@files) {
die "Invalid file name ($filename): $!" unless -e $filename;
}
close FILE_LIST;
open PATFILE, $ARGV[1]
or die "Cannot open pattern file: $!";
#Test the pattern file to make sure it looks okay, or die.
chomp(@patterns = <PATFILE>);
foreach $pattern (@patterns) {
#Pattern must begin with a forward slash, contain exactly three
#unescaped forward slashes, and end with same plus optional g and/or
+ i.
unless ($pattern =~ /^\/.*[^\\]\/.*[^\\]\/(g|i|gi|ig)?$/) {
die "<PATFILE>: bad pattern: $@";
}
#Create the substitution commands.
$pattern = 's' . $pattern;
}
close PATFILE;
#Create backup directory, timestamped to make it unique.
my $bakdir = "bak_${now}";
mkdir $bakdir, 0755 or die "Cannot create archive directory: $!";
my $tarFile = $bakdir . '/' . "bak.tar";
my $logfile = $bakdir . '/' . "logfile";
#Create log file
open LOG, "> $logfile"
or die "Cannot create logfile: $!";
select LOG;
$| = 1; # Don't keep LOG entries sitting in the buffer.
select STDOUT;
#Tarball the files to archive current state
!(system "tar", "chvlf", $tarFile, "-I", $ARGV[0])
or die "Cannot create backup archive!";
print LOG "Created archive OK.\n";
!(system "gzip", $tarFile)
or print LOG "Cannot compress archive; proceeding anyway.\n";
#Loop through each file and make the edit(s)
foreach $filename (@files) {
#Copy contents of file to a temporary work file.
my $wkfile = $bakdir . '/' . basename $filename;
copy($filename, $wkfile) or die "Cannot copy ${filename}: $!";
open(FILE2CHANGE, "<$wkfile") or die "Cannot open ${wkfile} for read
+ing: $!";
open(UPDATED, ">$filename") or die "Cannot open ${filename} for writ
+ing: $!";
while (<FILE2CHANGE>) {
#Run each substitution on the line.
foreach $pattern (@patterns) {
eval $pattern;
}
print UPDATED;
}
print LOG "Processed ${filename}\n";
close FILE2CHANGE;
close UPDATED;
unlink $wkfile;
}
close LOG;
###################################
sub usage {
die <<EOF
usage: fled [list file] [pattern file]
List file is a file containing the list of
files to process, one file name per line.
Pattern file must contain the substitution expressions,
one per line, using forward slashes as delimiters, but
omitting the 's' at the beginning (e.g. "/foo/bar/g").
Each line may optionally end with 'g' and/or 'i' switch(es).
EOF
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.