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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.