#!/usr/bin/perl -i.bak ############################################################################## # Mass Substitions in one or more files. # old files saved with .bak appended # Author: Chris Rouch # Date: 23/06/98 # Changelog: # 9/6/2003 added -l and -u # Copyright (c) Chris Rouch 1998-2003 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # or see http://www.gnu.org/licenses/gpl.txt ############################################################################## use strict; use Getopt::Long; # general global variables my ($PROG,$USAGE,$OPTIONS); use vars qw($old $new); # constants # command line variables my $ignore; #ignore case my $lower; #force lower case my $upper; #force upper case my $quote; # quote metechars # command line switch my %opts= ( "i|ignore" => \$ignore, "l|lower" => \$lower, "u|upper" => \$upper, "q|quote" => \$quote, ); ($PROG=$0) =~ s#.*/##; SetUsage(\%opts,\$OPTIONS); $USAGE="Usage: $PROG $OPTIONS old new file1 [file2...[filen]]"; die "$USAGE\n" unless GetOptions(%opts); # program begins here if ((!$upper) and (!$lower)) { if ($#ARGV >=2) { $old=shift(@ARGV); $new=shift(@ARGV); if ($quote) { $old=quotemeta($old); $new=quotemeta($new); } } else { die "$USAGE\n"; } } # else upper, lower or nice while (<>) { if ($old) { if ($ignore) { s/$old/$new/gi; } else { s/$old/$new/g; } } $_ = "\U$_" if ($upper); $_ = "\L$_" if ($lower); print; } ############### end of main ############# sub SetUsage { my ($ropts,$rstr) = @_; my $short; foreach my $key (keys %$ropts) { if ($key =~ /\|/) { ($short,$key) = split(/\|/,$key); } if ($key =~ /=/) { $key =~ s!=.*$! value!; } $key = '-' . $key; if ($short) { $short = '-' . $short; $key = $short . '|' . $key } $$rstr .= '[' . $key . '] '; } } ############### end of file #############