home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / question / 15922 < prev    next >
Encoding:
Internet Message Format  |  1993-01-25  |  1.3 KB

  1. 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
  2. From: aclark@netcom.com (Al Clark)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: How to add a list of numbers?
  5. Message-ID: <1993Jan25.171527.9094@netcom.com>
  6. Date: 25 Jan 93 17:15:27 GMT
  7. References: <1993Jan24.120619.5072@mark-one.inet-uk.co.uk>
  8. Organization: Self
  9. Lines: 27
  10.  
  11. In article <1993Jan24.120619.5072@mark-one.inet-uk.co.uk> mark@mark-one.inet-uk.co.uk (Mark Powell) writes:
  12. >What is the best (least CPU time) to addup a list of ASCII numbers?
  13. >eg you've just used cut to extract a list of file sizes from a ls -l
  14. >listing and now want to add all the numbers together. Is there some
  15. >utility that will do this?
  16. >All I can see is to write a for loop to add each number in turn to a
  17. >total, using expr.
  18. >Thanks in advance.
  19. >
  20. >-- 
  21. >Mark Powell
  22.          I don't know if it's the most efficient, but it's efficient
  23.      enough for me.
  24.      N=5
  25.      M=15
  26.      SUM=`echo "$M + $N" | bc`
  27.  
  28.      You can do any of the bc operations, include base and scale
  29.      operations.  Eg, hex arithmetic.
  30.      SUM=`echo "ibase=16; obase=16; $M + $N" | bc`
  31.      see bc(1) 
  32.  
  33.      Note:  the bc supplied with many systems is a pre-processor for
  34.      'dc'.  You can get the Gnu bc which is more efficient.
  35.  
  36. -- 
  37. Al - aclark@netcom.com - My opinions are my own.
  38.