#!/usr/bin/perl -w ############################################################ # THIS SOFTWARE COMES WITH NO WARRANTY ! # USE IT AT YOUR OWN RISK. IN NO CASE THE # AUTHOR WILL BE RESPONSIBLE FOR ANY DAMAGES # YOU MAY GET FROM THE DIRECT OR INDIRECT USE # OF THIS PROGRAM ############################################################ ############################################################ ## The global variables ############################################################ my $reg_exp = ""; my $prefix = ""; my $suffix = ""; my @file_list = (); my $line_by_line = 0; sub replace_line() { my $tmp_file = "/tmp/temp-file"; foreach $fname (@file_list) { print "Searching: '$fname'\n"; open FDR, "< $fname"; open FDW, "> $tmp_file"; my $matched = 0; while () { my $line = $_; my $line_new = $line; eval '$line_new =~ ' . "$reg_exp"; if ($line_new ne $line) { $matched = 1; } print FDW $line_new; } close FDR; close FDW; if ($matched) { my $fname_new = $fname; if ($prefix) { $fname_new = $prefix . $fname_new; } if ($suffix) { $fname_new = $fname_new . $suffix; } my $result = `cp -f $tmp_file $fname_new`; print "Processed: '$fname' ==> '$fname_new'\n"; } } } sub replace() { my $tmp_file = "/tmp/temp-file"; foreach $fname (@file_list) { print "Searching: '$fname'\n"; open FDR, "< $fname"; my $file = ""; while () { $file .= $_; } close FDR; my $file_new = $file; eval '$file_new =~ ' . "$reg_exp"; if ($file_new ne $file) { open FDW, "> $tmp_file"; print FDW $file_new; close FDW; my $fname_new = $fname; if ($prefix) { $fname_new = $prefix . $fname; } elsif ($suffix) { $fname_new = $fname . $suffix; } my $result = `cp -f $tmp_file $fname_new`; print "Processed: '$fname' ==> '$fname_new'\n"; } } } sub print_help() { print < SYNOPSIS replace.pl -r [-l] [-p
] [-s ]  [ ...]
  replace.pl [-h | --help]

DESCRIPTION
  This program applies the regular expression  on
  every file specified on the command line.
  If the -l option is not specified, each file is read into
  a single Perl string and the substitution is applied.
  If the -l option is specified, the substitution is applied
  separately on each line of each file.
  The substituted file overwrites the original one, unless
  the -p or -s options are used, so pay attention before
  using this command.
  An output is generated (either overwriting the original
  files, or creating new ones) only for files that match the
  regular expression.

OPTIONS
  -h | --help	Display this help message.
  -p 	Adds the prefix  to the destination file names,
                so does not overwrite the original ones.
  -s 	Adds the suffix  to the destination file names,
                so does not overwrite the original ones.
  -r 	Specifies the Perl regular expression to apply. Type
                "man perlre" for further information.
  -l            Apply the regular expression line by line.

NOTE
  Options can be mixed with file names, so "-h", "--help",
  "-p", "-r", "-s" and "-l" are never recognized as filenames.

EXAMPLES

- Replace each occurrence of 'foo' with 'bar' and prefix output files
  with 'bar_':

    replace.pl -r 's/foo/bar/g' -p bar_ foo*.txt

- Replace the first occurrence of 'foo' in each line with 'bar'
  and overwrite the original files:

    replace.pl -l -r 's/foo/bar/g' -p bar_ foo*.txt

- Example use of match references:

    replace.pl -r 's/a(.*)b/A\\1B/g' *.txt

END
  exit(0);
}

############################################################
## The entry point
############################################################

my $param = shift;
while (defined $param) {
    if ($param eq "-r") {
	$reg_exp = shift;
    } elsif ($param eq "-l") {
	$line_by_line = 1;
    } elsif ($param eq "-p") {
	$prefix = "" . shift;
    } elsif ($param eq "-s") {
	$suffix = "" . shift;
    } elsif (($param eq "-h") || ($param eq "--help")) {
	print_help();
    } elsif ($param ne "") {
	@file_list = (@file_list, $param);
    }
    $param = shift;
}

if (scalar(@file_list) == 0) {
    print "Error: missing file name(s) !\n";
    exit(-1);
} elsif ($reg_exp eq "") {
    print "Error: missing regular expression !\n";
    exit(-1);
}

print "Applying regular expression '$reg_exp'\n";
if ($prefix ne "") {
    print "Prefixing output files with '$prefix'\n";
}
if ($suffix ne "") {
    print "Adding suffix '$prefix' to output file names.\n";
}
print "Source files: (" . join(', ', @file_list) . ")\n";

if ($line_by_line == 1) {
    replace_line();
} else {
    replace();
}

exit(0);