Hi All, I have two files. File1 Master.txt and file2 update.txt. I want to look at both files and find a matching key ex: $key = substr($line,3,12); If a key from the update.txt file is found in the master, replace the entire line in the master. If the key from update is not found in the Master.txt, add the line from the update.txt to the master.txt. it sounds really simple but I am strugling with it. Help or thought would be greatly appreciated.

sub updateMSTR { open(SEC,"pw".$prev_date.".sec")||warn("sec file not found")&& exi +t; open (MSTR,"c:\\lab\\PWB\\Master\\Security_Master.sec"); open( NewMSTR, ">MASTER.TMP"); while(defined($line1 = (<SEC>))){ chomp($line1); $key1 = substr($line1,3,12); $key1 =~s/\s+//g; while(defined($line2 = (<MSTR>))){ chomp($line2); $key2 = substr($line2,3,12); $key2 =~s/\s+//g; if ("$key1" eq "$key2"){ print"$key $key2\n"; print NewMSTR "1 $line1\n"; }else{ print NewMSTR "2 $line2\n"; } } } close(MSTR); close(NewMSTR); }

In reply to compare two files and update by mmittiga17

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.