[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: summation in dc
From: |
Russ Allbery |
Subject: |
Re: summation in dc |
Date: |
Wed, 11 Aug 2004 13:02:14 -0700 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Security Through Obscurity, linux) |
Mike Maxwell <maxwell@ldc.upenn.edu> writes:
> A frequent task I have is to sum a series of numbers. The 'dc' utility
> is a nice tool, but I don't see any obvious way of using it to sum a
> list of arbitrary length. Well, I guess I could use wc -l to find out
> how many numbers I was going to sum, then keep decrementing that, but
> that seems clumsy...
> I can read the numbers into dc, then keep hitting the "+" key until I
> get a "stack empty" error, then do "p". But that also seems clumsy, and
> besides, I want this to run automatically, without keystrokes, or me
> waiting to see the error msg.
> Maybe there's a way of doing this with macros in dc? Or in bc? Or in
> some other tool? If the latter, it needs to be s.t. readily available,
> as I want it to work in several different environments (CygWin, Linux,
> FreeBSD).
Sounds like you're looking for something like:
awk '{ total += $1 } END { print total }' file
where file contains all your numbers, one per line.
--
Russ Allbery (rra@stanford.edu) <http://www.eyrie.org/~eagle/>