Category: utilities
Author/Contact Info nop
Description: Small script to generate frequency counts for any given field in a fixed-format textfile. Useful for summarizing and checking flat files or mailing lists.
# fieldcount.pl
# go thru a file and give counts by some field
# usage:  perl fieldcount.pl -s [startcol] -e [endcol] filename 

sub message {print STDERR "$. lines counted, col $s to $e in $ARGV\n";
+}

use Getopt::Long;  
GetOptions("s=i" => \$s, "e=i" => \$e);
$l = 1+$e-$s;
if ($e<0 || $s<0 || $l < 1) {die "usage: perl $0 -s startcol -e endcol
+ myfile.txt\n";}
$s0=$s-1; 
while (<>) {
  if (($. % 10000) ==1) {&message;}
  $n{substr($_, $s0,$l)}++; $tot++;
}
&message;
foreach (keys %n) {
 print "$_\t$n{$_}\n";
}
print "total $tot\n";