I have this code. This code is functioning well to find the common lines between multiple files. It was just, I dint have any idea how to sort the output from the highest repetition to the lowest. Instead of 5,3,2,6,4,5,6 I want the files to be sorted out as 6,6,5,5,4,3,2
The Output.txt
For line --> five
This line occurs 5 times in the following files: -
a.txt,
b.txt,
c.txt,
d.txt,
e.txt
For line --> three
This line occurs 3 times in the following files: -
a.txt,
b.txt,
c.txt
For line --> two
This line occurs 2 times in the following files: -
a.txt,
b.txt
For line --> eight
This line occurs 6 times in the following files: -
a.txt,
b.txt,
c.txt,
d.txt,
e.txt,
f.txt
For line --> four
This line occurs 4 times in the following files: -
a.txt,
b.txt,
c.txt,
d.txt
For line --> six
This line occurs 5 times in the following files: -
a.txt,
b.txt,
c.txt,
d.txt,
e.txt
For line --> seven
This line occurs 6 times in the following files: -
a.txt,
b.txt,
c.txt,
d.txt,
e.txt,
f.txt
The total common line between files are 7
The Script files (perl)
#!/usr/bin/perl -w
my %hash;
my $file;
my $fh;
my $count;
for $file (@ARGV) {
open ($fh, $file) or die "$file: $!\n";
while(<$fh>) {
push @{$hash{ $_}}, $file;
}
}
for (keys %hash) {
$n = @{$hash{$_}};
if(@{$hash{$_}} > 1) {
$count ++;
print "\n For line --> $_\n";
print "This line occurs $n times in the following files: - \n", join(",\n", @{$hash{$_}}), "\n\n";
}
}
print "The total common line between files are $count\n";
exit 0;