#!/bin/sh -- # A comment mentioning perl, to prevent perl from looping. Indented to work with bash. eval 'exec perl -S $0 ${1+"$@"}' if 0; $n = 0; $len = 0; $symbol = '*'; $number = 0; $width = 0; $memworry = 0; $fillin = 0; chop($Program = `basename $0`); $Usage = <<"END"; $Program: Print out a crude histo Usage: $Program Options: -n Number lines -m Worry about memory -f Reduce by factor -F fill in missing values. Only works for ints. -s Use symbol for plot -w Fill terminal width -b use integer bins Notes: END LOOP: while (@ARGV) { $_ = shift; CASE: { /^-m$/ && ($memworry = 1, last CASE); /^-n$/ && ($number = 1, last CASE); /^-w$/ && ($width = 1, last CASE); /^-F$/ && ($fillin = 1, last CASE); /^-f$/ && ($factor = shift, last CASE); /^-b$/ && ($bin = shift, last CASE); /^-s$/ && ($symbol = shift, last CASE); /^--$/ && (last LOOP); # -- means end of switches /^-(-.*)$/ && (unshift(@ARGV, $1), last CASE); /^(-h|-help)$/ && ((print $Usage), exit 0, last CASE); if ( /^-(..+)$/ ) { # split bundled switches: local($y, $x) = ($1, ''); foreach $x (reverse(split(//, $y))) { unshift(@ARGV,"-$x") }; last CASE; } /^-/ && ((print "Invalid arg: $_\n$Usage"), exit 1, last CASE); unshift(@ARGV,$_); last LOOP; } } if ( $width ) { chop($width = `stty -a &1| grep -i columns | head -1`); if ( $width =~ /columns\s+(\d+)\b/ ) { $width = $1; $width -= 5; } elsif ( $ENV{COLUMNS} ) { $width = $ENV{COLUMNS}; $width -= 5; } else { $width = 0; print STDERR "Could not find terminal width.\n"; } } $Count_Max = 0; while (<>) { chop; $key = $_; $key =~ s/^\s*//; $key =~ s/\s*$//; if ($bin) { $key = $bin * ( int($key/$bin) ); } if ( ++$Count{$key} > $Count_Max ) { $Count_Max = $Count{$key}; } if ( $memworry ) { $Keys{$key} = $key; } else { $Keys[$n++] = $key; } if( length($key) > $len) { $len = length($key); } } print STDOUT "\n"; if ( $memworry ) { $n = 0; foreach $key (sort keys %Keys) { $Keys[$n++] = $key; } } if ( $width > $len + 10 ) { $width -= $len + 3; } for ($i=0; $i < $n; $i++) { $key = $Keys[$i]; if ( $Did_key{$key} eq '' ) { $Did_key{$key} = 'T'; $tag = $Keys[$i]; for($j = length($tag); $j < $len; $j++) { $tag .= ' '; } $tag .= ': '; $max = $Count{$key}; if ( $width && $Count_Max ) { $max = int( $width * (($max + 0.) / $Count_Max) ); } if ( $factor ne '' ) { $max = int($max/$factor); } for($j = 0; $j < $max; $j++) { $tag .= $symbol; } if ( $number ) { ($x, $tag) = split(/\s*:\s*/, $tag); $tag = length($tag); $tag = "$x $tag"; } print STDOUT $tag."\n"; } }