I am a newbie to perl. I want to compare two text files and print the difference to a third text file. I am not able to find out the required perl script. can somebody please help. The following is my code,
use File::Compare;
my $F1="version1.txt";
my $F2="count.txt";
my $F3="differ.txt";
if(compare($F1,$F2)==0)#the addition of the USE line is important for
+this function to work.
{print"they are the same";}
elsif(compare($F1,$F2)==1)
{print"they are different";
#opening/creating all three
open (FH1,$F1)||die "cannot open $F1.\n";
open (FH2,$F2)||die "cannot open $F2.\n";
open (FH3,'>',$F3)||die "cannot create/open $F3.\n";
while (<FH1>)
{
last if eof(FH2);
$content2=<FH2>; #reads the complete file to content2.
chomp($_, $content2);
#haven't yet figured out how to pull out the difference data.
}
close(FH1);
close(FH2);
close(FH3);
}
elsif(compare($F1,$F2)==-1)
{print"error";}
else {print"something wrong";}
print"Please check the file differ.txt";
Discipulus added code tags
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.