#!/bin/sh # # produce a simple ascii scaled bar-plot of input values. # # Usage: barplot [-nc #] [-lc #] [-lw #] [-pw #] [-is] [files] # -nc/-lc set number/label column, respectively # -lw/-pw set label/plot width, respectively # -sn set value to subtract from each value to be plotted # -is set initial maximum value (to force plotting to a known scale) # # Corey Satten, corey @ cac.washington.edu, 2/12/92 # http://staff.washington.edu/corey LABWIDTH=8 # width of value field at left PLOTWIDTH=79 # width of entire plot LCOL=1 # default label column NCOL=1 # default number column SN=0 # default number to subtract MAX=0 # forced maximum for i in $*; do case "$1" in -lc) LCOL=$2; FLD=$2; shift; shift;; # label from column # -nc) NCOL=$2; FLD=$2; shift; shift;; # number from column # -is) MAX=$2; FLD=$2; shift; shift;; # initial maximum -lw) LABWIDTH=$2; shift; shift;; # value field width -pw) PLOTWIDTH=$2; shift; shift;; # scale plot to width -sn) SN=$2; shift; shift;; # subtract from each # -*) echo unknown flag 1>&2; exit 1;; *) break;; # end option parsing esac done awk " BEGIN { max = $MAX } { num = \$($NCOL) - $SN # subtract SN from all input nums if (num < min) min = num # compute minimum input number if < 0 if (num > max) max = num # compute maximum input number val[NR] = num # save input number value lab[NR] = \$($LCOL) # save input label value } END { dashes = \"------------------------------------------------------\" while (length(dashes) < $PLOTWIDTH) dashes = dashes dashes scale = ($PLOTWIDTH-$LABWIDTH-1)/(max-min) for (i=1; i <= NR; ++i) { len = int((val[i]-min)*scale+.5); printf (\"%-$LABWIDTH.${LABWIDTH}s%s|\\n\", \ lab[i], substr(dashes,1,len)) } } " $*