#!/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 }

In reply to Edit a List of Files by lev36

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.