home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / question / 15972 < prev    next >
Encoding:
Text File  |  1993-01-27  |  2.7 KB  |  59 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!paladin.american.edu!howland.reston.ans.net!spool.mu.edu!sgiblab!rtech!amdahl!amdcad!amdint.amd.com!diablo!monson
  3. From: monson@diablo.amd.com (Steve Monson)
  4. Subject: Re: How to add a list of numbers?
  5. Message-ID: <C1GqHs.t1@amd.com>
  6. Sender: usenet@amd.com
  7. Organization: Advanced Micro Devices, Inc., Austin, TX
  8. References: <1993Jan24.120619.5072@mark-one.inet-uk.co.uk> <1993Jan25.171527.9094@netcom.com>
  9. Date: Tue, 26 Jan 1993 13:22:39 GMT
  10. Lines: 47
  11.  
  12. >In article <1993Jan24.120619.5072@mark-one.inet-uk.co.uk> mark@mark-one.inet-uk.co.uk (Mark Powell) writes:
  13. >What is the best (least CPU time) to addup a list of ASCII numbers?
  14. >eg you've just used cut to extract a list of file sizes from a ls -l
  15. >listing and now want to add all the numbers together. Is there some
  16. >utility that will do this?
  17. >All I can see is to write a for loop to add each number in turn to a
  18. >total, using expr.
  19.  
  20. Well, using our ls -l, I get something like this:
  21.  
  22. -rwx------  1 monson       9320 Aug  7 08:36 timesheet
  23. -rwx------  1 monson        349 Mar 24  1992 triplets
  24. -rwx------  1 monson       1258 Dec 17  1991 ts
  25. -rwx------  1 monson        473 Jun 17  1992 tu
  26. -rwx------  2 monson        263 Sep 16  1991 unrot
  27. -rwx------  1 monson         47 Apr  9  1991 unshar
  28. -rwx------  1 monson        647 Jan 27  1992 uum
  29. -rwx------  1 monson       1227 Oct  2 11:05 uumail
  30. -rw-------  1 monson        530 Apr 10  1992 uumail.sh
  31. -rwx------  1 monson       1006 Oct 30  1990 uumdecode
  32. -rwx------  1 monson        344 May 23  1991 wordcards
  33. -rwx------  1 monson       1045 Sep 23 12:21 xcursors
  34.  
  35. So I want to add up all the items in the 4th column. You can use "cut" to
  36. get the column entries, but multiple delimiters have to be compacted. So, the
  37. following command line runs ls, shrinks the white space to single blanks,
  38. extracts the 4th column, and changes all the blanks to "+" signs, and
  39. feeds the expression into bc, which prints the answer:
  40.  
  41. echo `ls -l | sed 's/  */ /g' | cut -d\  -f4` | sed 's/ /+/g' | bc
  42.  
  43. I suppose you could also use expr, but then you have to insert spaces
  44. around the plus signs, and make it all a single expression as arguments
  45. on the expr command line:
  46.  
  47. expr `echo \`ls -l | sed 's/  */ /g' | cut -d\  -f4\` | sed 's/ / + /g'`
  48.  
  49. but that starts to get messy, with the nested `` strings. Still, it avoids
  50. loops. Now, as to whether it's the "fastest," I have no idea. It does do a
  51. bunch of forks etc. to crank everything up. I suppose perl would be better;
  52. it often is.
  53.  
  54. Steve
  55. -- monson@diablo.amd.com -- (512) 462-4013
  56.  __     | signature designed by | If God were a liberal, we'd have the
  57. (_      | (and ripped off from) | Ten Suggestions.
  58. __)teve | Stephen Wayne Miller  |         James P. Hogan, _The Entoverse_
  59.