[ale] Shell script

Danny Cox danny at compgen.com
Thu Sep 28 07:01:03 EDT 2000


Brian,

On Wed, 27 Sep 2000, Brian J. Dowd wrote:
> Does anyone have a bash script which will
> sum all the numbers in a file?
> They aren't integers (which rules out 'expr')
> Each is on a separate line. Example:
> -45.23
> 55.6
> 123.44
> ...
> I can't seem to make 'bc' work...

	Something from a book, sorry, don't remember which book ;-):

colsum:
========================================================================
#!/bin/sh
# colsum --- sum column N
#
case $# in
	0) echo "Usage: $0 <column #> [file] ..." 1>&2; exit 1;;
esac

COL=$1
shift
awk '{total += $'$COL';} END {print total}' "$@"
========================================================================

	In your case, use it as:

		colsum 1 <num_file

	Something I used a lot at one time:

		ls -l | colsum 5

tells the # of bytes in a directory.

	It's a little more general, and even occasionally useful!

Danny

--
To unsubscribe: mail majordomo at ale.org with "unsubscribe ale" in message body.





More information about the Ale mailing list