Sep 14, 2007

Replace file contents by perl

#!/usr/bin/perl -w
# use strict;

sub replace_str($$$)
{
my $line = shift;
my $old_string = shift;
my $new_string = shift;

if($line =~ m/($old_string)/)
{
$line =~ s/$old_string/$new_string/;
}
return $line;
}

open (SOURCE_FILE, "source.txt");
open (DESTINATION_FILE, '>>destination.txt');

while ($line = <source_file>) {
$line = replace_str($line,"2","3");
print DESTINATION_FILE $line;
}

close(SOURCE_FILE);
close (DESTINATION_FILE);

No comments: