1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
use Getopt::Std; |
4 |
use FindBin;use lib $FindBin::Bin; |
5 |
|
6 |
my $usage = q/Usage: |
7 |
gettop.pl [-f <col#>] [-d <delim>] [-t N] sorted_lines.. |
8 |
|
9 |
Display only the top N (default 1) lines with the same string |
10 |
in column <col#> (default 1), where columns are delimited |
11 |
by <delim> (default: tab character). |
12 |
ATTENTION: the input lines must be already sorted by column <col#> |
13 |
AND the criterion of choice. |
14 |
/; |
15 |
umask 0002; |
16 |
getopts('f:d:t:') || die($usage."\n"); |
17 |
|
18 |
my $fld=$Getopt::Std::opt_f || 1; |
19 |
$fld--; |
20 |
my $delim=$Getopt::Std::opt_d || "\t"; |
21 |
my $top=$Getopt::Std::opt_t || 1; |
22 |
|
23 |
my ($prev, $count); |
24 |
while (<>) { |
25 |
chomp; |
26 |
my @f=split($delim); |
27 |
if ($f[$fld] eq $prev) { $count++ } |
28 |
else { $count=1 } |
29 |
print $_."\n" if $count<=$top; |
30 |
$prev=$f[$fld]; |
31 |
} |