in reply to How to extract duplicate lines from a text file?

Try this (more idiomatic) version:
#!/usr/bin/perl use strict; use warnings; my ($fname, $dupcount) = @ARGV; die "USAGE: extract_duplicate_rows <input file> <# of duplicate rows>\ +n\n" unless $fname and $dupcount; open(my $IFILE, "<", $fname) or die "Cannot open $fname: $! "; my @buf; while (my $line = <$IFILE>) { if (@buf){ if (substr($line,0,10) eq substr($buf[0],0,10)){ push @buf,$line; next }; if (@buf ==$dupcount){ print @buf; }; }; @buf=($line); } @buf==$dupcount and print @buf; close $IFILE;
This assumes that the input file is sorted.

     Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Replies are listed 'Best First'.
Re^2: How to extract duplicate lines from a text file?
by rnaeye (Friar) on Mar 01, 2011 at 13:28 UTC

    Thanks a lot for the help.