mmittiga17 has asked for the wisdom of the Perl Monks concerning the following question:
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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: compare two files and update
by kcott (Archbishop) on Nov 05, 2010 at 22:53 UTC | |
|
Re: compare two files and update
by MishaMoose (Scribe) on Nov 05, 2010 at 19:48 UTC | |
|
Re: compare two files and update
by umasuresh (Hermit) on Nov 05, 2010 at 19:58 UTC | |
|
Re: compare two files and update
by ig (Vicar) on Nov 06, 2010 at 18:08 UTC | |
by mmittiga17 (Scribe) on Nov 12, 2010 at 18:43 UTC | |
|
Re: compare two files and update
by MishaMoose (Scribe) on Nov 06, 2010 at 15:31 UTC |