: ########################################################################## # Title : summe - calculate the sum of a column # Author : Heiner Steven # Date : 1994-05-28 # Requires : awk # Category : Text Utilities # SCCS-Id. : @(#) summe 1.3 07/09/17 ########################################################################## # Description # ########################################################################## PN=`basename "$0"` # Program name VER='1.3' Col=1 # Field number for sum Delim= # Field delimiter usage () { echo >&2 "$PN - calculate the sum, $VER (stv '94) usage: $PN [-f field] [-t delim] [file ...] -f: specify column (default is $Col) -t: field delimiter (default is whitespace)" exit 1 } msg () { for i do echo "$PN: $i" >&2 done } fatal () { msg "$@"; exit 1; } while [ $# -gt 0 ] do case "$1" in -f) Col="$2"; shift;; -t) Delim="$2"; shift;; --) shift; break;; -h) usage;; -*) usage;; *) break;; # First file name esac shift done case "$Col" in *[!0-9]) fatal "no column number: $Col";; esac awk ${Delim:+-F "$Delim"} ' (NF >= '$Col') { s += $'$Col' } END { printf "%.0f\n", s } ' "$@"