PERL search and replace

a simple script i made to replace multiple appearance of a word in a raw or multiple raws, output to file.new

#!/usr/bin/perl

if ($#ARGV != 2) {

  print “this programs output is FILENAME.new\n”;

  print “usage: search_Replace.pl

     BEFORE AFTER FILENAME\n”;

  exit;

}

$newfile_name = “$ARGV[2].new”;

print “Before : “,$ARGV[0],”\n”;

print “After  : “,$ARGV[1],”\n”;

print “Opening … “;

open (OLDF, $ARGV[2]) ||

   die (“Could not open file. $!”);

open (NEWF, “>$newfile_name”) ||

   die (“Could not create output file. $!”);

print “IN/OUT pipes are open.\n”;

while ($text = ){

    $text =~ s/$ARGV[0]/$ARGV[1]/g;

    print NEWF “$text”;

    }

close (OLDF);

close (NEWF);

Leave a Reply