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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |