Scripts: colsum sums the specified column

master
Pierre Neidhardt 2014-03-06 15:21:53 +01:00
parent cb030e8710
commit 1471942ed1
1 changed files with 20 additions and 0 deletions

20
.scripts/colsum Executable file
View File

@ -0,0 +1,20 @@
#!/bin/sh
COLUMN="1"
FORMAT="TOTAL: %s\n"
if [ "$1" = "-h" ]; then
cat<<EOF
Usage: ${0##*/} [COLUMN] [FORMAT]
Sum COLUMN (default to column $COLUMN) and output result with format FORMAT
(default to $FORMAT).
EOF
exit
fi
[ $# -ge 1 ] && COLUMN="$1" && shift
[ $# -ge 1 ] && FORMAT="$1"
awk -v format="$FORMAT" '{total += $'$COLUMN'; print} END {printf format, total}'