#! /usr/bin/python """Usage: colsum.py column-number [input-files ...] """ import fileinput, string, sys def die(reason, file=None, line=None): if file: fileline = "file %s line %d: " % (file, line) else: fileline = "" sys.stderr.write(fileline + reason + '\n') sys.exit(1) total = 0 try: colnum = int( sys.argv.pop(1) ) - 1 except IndexError: die(__doc__) for lin in fileinput.input(): items = string.split(lin) file, linno = fileinput.filename(), fileinput.filelineno() try: field = items[colnum] number = int(field) total = total + number except IndexError: die("not enough fields", file, linno) except ValueError: die("unable to parse number %s" % `field`, file, linno) print total