Skip to content

Instantly share code, notes, and snippets.

@ruel
Created November 24, 2010 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruel/713917 to your computer and use it in GitHub Desktop.
Save ruel/713917 to your computer and use it in GitHub Desktop.
This script identifies the duplicate lines with their corresponding line numbers within a file.
#!/usr/bin/perl
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (c) 2010 Ruel Pagayon <ruel@ruel.me> - http://ruel.me
use strict;
use warnings;
sub loadf($) {
my @file = ( );
open(FILE, $_[0] . "\n") or die("Couldn't Open " . $_[0] . "\n");
@file = <FILE>;
close(FILE);
return @file;
}
{
my @file = loadf("path-to-file.txt");
my @inner = @file;
my @dup = ( );
my $l0 = 0; my $l1 = 0; my $l2 = 0; my $dc = 0; my $tc;
foreach my $line (@file) {
$l1++;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
foreach my $iline (@inner) {
$l2++;
$iline =~ s/^\s+//;
$iline =~ s/\s+$//;
next if ($l1 == $l2 || grep { $_ eq $l1} @dup );
if ($iline eq $line) {
$dc++;
if ($dc > 0) {
if ($l0 == 0) {
print "Line " . $l1 . ": " . $line . "\n";
$l0++;
}
print "Line " . $l2 . ": " . $iline . "\n";
push (@dup, $l2);
}
}
}
print "\n" unless($dc == 0);
$dc = 0; $l0 = 0; $l2 = 0;
}
}
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment