home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!howland.reston.ans.net!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!olivea!hal.com!decwrl!csus.edu!netcom.com!aclark
- From: aclark@netcom.com (Al Clark)
- Newsgroups: comp.unix.questions
- Subject: Re: How to add a list of numbers?
- Message-ID: <1993Jan25.171527.9094@netcom.com>
- Date: 25 Jan 93 17:15:27 GMT
- References: <1993Jan24.120619.5072@mark-one.inet-uk.co.uk>
- Organization: Self
- Lines: 27
-
- In article <1993Jan24.120619.5072@mark-one.inet-uk.co.uk> mark@mark-one.inet-uk.co.uk (Mark Powell) writes:
- >What is the best (least CPU time) to addup a list of ASCII numbers?
- >eg you've just used cut to extract a list of file sizes from a ls -l
- >listing and now want to add all the numbers together. Is there some
- >utility that will do this?
- >All I can see is to write a for loop to add each number in turn to a
- >total, using expr.
- >Thanks in advance.
- >
- >--
- >Mark Powell
- I don't know if it's the most efficient, but it's efficient
- enough for me.
- N=5
- M=15
- SUM=`echo "$M + $N" | bc`
-
- You can do any of the bc operations, include base and scale
- operations. Eg, hex arithmetic.
- SUM=`echo "ibase=16; obase=16; $M + $N" | bc`
- see bc(1)
-
- Note: the bc supplied with many systems is a pre-processor for
- 'dc'. You can get the Gnu bc which is more efficient.
-
- --
- Al - aclark@netcom.com - My opinions are my own.
-