#!/usr/bin/perl use strict; # A hack to count a column my $col = 0; my $align = 0; my $numeric = 0; while( $_ = pop( @ARGV ) ) { (/\d+/) && ($col = $_); (/-v/) && ($align = 1); (/-n/) && ($numeric = 1); } print STDERR "Summing column $col\n"; my %counter; while( ) { split; $counter{@_[$col]}++; } # Print the results (!$align && printhor()) || printver(); sub printver { if( $numeric ) { foreach (sort {$a <=> $b} keys %counter) { print $_, "\t", $counter{$_}, "\n"; } } else { foreach (sort keys %counter) { print $_, "\t", $counter{$_}, "\n"; } } } sub printhor { if( $numeric ) { foreach (sort {$a <=> $b} keys %counter) { print; print "\t"; } print "\n"; foreach (sort {$a <=> $b} keys %counter) { print $counter{$_}, "\t"; } print "\n"; } else { foreach (sort keys %counter) { print; print "\t"; } print "\n"; foreach (sort keys %counter) { print $counter{$_}, "\t"; } print "\n"; } }