Advertisement
therube

addup unix script

May 24th, 2011
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 2.57 KB | None | 0 0
  1. #
  2. #       @(#) addup.sh 1.0 90/07/19
  3. #
  4. #       Copyright (C) Steven J. Bensky, 1990
  5. #       Adds up a column (default=last) of numbers in a file.
  6. #       95/05/16 updated to allow (999) negative style numbers.
  7.  
  8. case $1 in
  9. -[0-9])
  10.         COLUMN=`echo $1 | tr -d -`
  11.         shift
  12. ;;
  13. *)
  14.         COLUMN="NF"
  15. ;;
  16. esac
  17. echo "Adding up column .. $COLUMN .. of file(s) .. $*"
  18.  
  19. nawk  ' OFMT="%.2f"                                       # 1 "%12.2f"
  20.         { x = '$COLUMN'                                   # 2
  21.           neg = index($x, "$")                            # 3
  22.           if (neg > 0) X = gsub("\\$", "", $x)
  23.           neg = index($x, ",")                            # 4
  24.           if (neg > 1) X = gsub(",", "", $x)
  25.           neg = index($x, "(")                            # 8 neg (123 & change
  26.           if (neg > 0) X = gsub("\\(", "", $x)
  27.           if (neg > 0) $x = (-1 * $x)                     # it to "-123.00"
  28.           neg = index($x, "-")                            # 5
  29.           if (neg > 1) $x = (-1 * $x)                     # 6
  30.           t += $x                                         # 7
  31.           print "x is <<<", $x+0, ">>> running balance:", t
  32.         } ' $*
  33.  
  34. # 1.  set numeric format to eliminate rounding errors
  35. # 1.1 had to reset numeric format from 12.2f to .2f 95/05/16
  36. #     when a computed number is assigned to a variable ( $x = (-1 * $x) )
  37. #     it causes $x to use the OFMT so -1.23 = "________-1.23" vs "-1.23"
  38. #     and that causes my #5 (negative check) to not work correctly because
  39. #     the index returns a number >1 and to the neg neg than becomes a positive
  40. #     this only occurs if the number happened to b a "(" neg number
  41. # 2.  find the field we want to add up (comes from the shell or defaults
  42. #     to the last field "NF") in the file
  43. # 3.  check for a dollar sign ($) in the number - if there get rid of it
  44. #     so we may add it correctly - $12 $1$2 $1$2$ $$1$$2$$ all = 12
  45. # 4.  check for a comma (,) in the number - if there get rid of it so we
  46. #     may add it correctly - 1,2 12, 1,,2 1,,2,, all = 12   (,12=0)
  47. # 5.  check for negative numbers
  48. # 6.  if x is a negative number in the form 999- "make" it a recognized
  49. #     number like -999 - if x is a negative number like -999 already
  50. #     the test fails (y is not >1) and this "true" negative is not made
  51. #     positive
  52. # 7.  accumulate the total
  53. # 8.  if x is a negative number in the form (999) "make it a recognized
  54. #     number like -999
  55. # * Note that a (-9) (neg neg number) returns a postive
  56. # * Mite not work rite with all forms of all numbers using $-,+. etc. *
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement