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;
In reply to need help in editing multiple word in a file by vkp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |