J
jan09876
A friend wrote for me this little script so I can compare two files
and have this output.
Common to all files:
====================
Only in '1.txt':
====================
Only in '2.txt':
====================
I did not use the script for a while and now I am not able to find out
what the command was. I tried different commands but the script keep
on saying: No such file or directory. I have 1.txt and 2.txt as files
that I want to compare.
../same.pl -in1 1.txt -in2 2.txt -same out1.txt -diff out2.txt
Many thanks for your help
#!/usr/bin/perl
use strict;
use Getopt::Long;
my $USAGE =
"same.pl [--in1 inputfile1 --in2 inputfile2 --same outputSameWords --
diff outputDiffWords]
Compares the 2 in files and checks the same and different words";
my %opts;
GetOptions(\%opts, qw(in1=s in2=s same=s diff=s)) || die $USAGE;
my $fs1 = $opts{in1};
my $fs2 = $opts{in2};
my $out1 = $opts{same};
my $out2 = $opts{diff};
main();
print "done!";
sub main {
open (OUT1, ">out1.txt");
open (OUT2, ">out2.txt");
parse();
close OUT1;
close OUT2;
}
sub parse {
my $_words1 = getWords($fs1);
my $_words2 = getWords($fs2);
for (sort keys %$_words1) {
if (exists $_words2->{$_}) {
print OUT1 "$_\n";
}
else {
print OUT2 "$_\n";
}
}
}
sub getWords {
my $fs = shift;
my $txt = getFile($fs);
my %words;
while ($txt =~ m/(\w+)/g) {
$words{$1} = 1;
}
return \%words;
}
sub getFile {
my $fs = shift;
open (IN, $fs) or die "cant open $fs";
my $txt = do{local $/;<IN>};
return $txt;
}
and have this output.
Common to all files:
====================
Only in '1.txt':
====================
Only in '2.txt':
====================
I did not use the script for a while and now I am not able to find out
what the command was. I tried different commands but the script keep
on saying: No such file or directory. I have 1.txt and 2.txt as files
that I want to compare.
../same.pl -in1 1.txt -in2 2.txt -same out1.txt -diff out2.txt
Many thanks for your help
#!/usr/bin/perl
use strict;
use Getopt::Long;
my $USAGE =
"same.pl [--in1 inputfile1 --in2 inputfile2 --same outputSameWords --
diff outputDiffWords]
Compares the 2 in files and checks the same and different words";
my %opts;
GetOptions(\%opts, qw(in1=s in2=s same=s diff=s)) || die $USAGE;
my $fs1 = $opts{in1};
my $fs2 = $opts{in2};
my $out1 = $opts{same};
my $out2 = $opts{diff};
main();
print "done!";
sub main {
open (OUT1, ">out1.txt");
open (OUT2, ">out2.txt");
parse();
close OUT1;
close OUT2;
}
sub parse {
my $_words1 = getWords($fs1);
my $_words2 = getWords($fs2);
for (sort keys %$_words1) {
if (exists $_words2->{$_}) {
print OUT1 "$_\n";
}
else {
print OUT2 "$_\n";
}
}
}
sub getWords {
my $fs = shift;
my $txt = getFile($fs);
my %words;
while ($txt =~ m/(\w+)/g) {
$words{$1} = 1;
}
return \%words;
}
sub getFile {
my $fs = shift;
open (IN, $fs) or die "cant open $fs";
my $txt = do{local $/;<IN>};
return $txt;
}