vkp has asked for the wisdom of the Perl Monks concerning the following question:
i want to replace some words in a file. In words are present in whole file. But starting of each line in file is by different word. so i am using a file in which first character of line at which i wanna do replacement is written and along with it replaced word is given ( i name it match file) . i m using following logic. but here is problem with it that i don't know in which line the first match-replace combination in match file is present in input file. so i need to reopen this file. In this way code will copy all the lines in new file that i m forming with this.
#!/usr/bin/perl use warnings; my $source = shift @ARGV; my $destination = shift @ARGV; my $matchfile=shift @ARGV; open(OUT, '>', $destination) or die $!; open(MF, '<', $matchfile) or die $; while(<MF>) { @DATA=split(/ /,$_); $matcher=$DATA[0]; $replacer=$DATA[1]; open(IN, '<', $source) or die $!; while(<IN>) { if($_=~/^$matcher/) { if(s /legend/$replacer/) { print OUT $_; } } else { print OUT $_; } } } close IN; close OUT; close MF;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: need help in editing multiple word in a file
by lancer (Scribe) on Nov 18, 2012 at 21:06 UTC | |
by vkp (Novice) on Nov 19, 2012 at 17:33 UTC | |
Re: need help in editing multiple word in a file
by pvaldes (Chaplain) on Nov 18, 2012 at 21:12 UTC | |
by vkp (Novice) on Nov 19, 2012 at 17:40 UTC | |
by Not_a_Number (Prior) on Nov 19, 2012 at 19:10 UTC | |
Re: need help in editing multiple word in a file
by aitap (Curate) on Nov 18, 2012 at 18:52 UTC | |
Re: need help in editing multiple word in a file
by pvaldes (Chaplain) on Nov 20, 2012 at 12:43 UTC |
Back to
Seekers of Perl Wisdom