expense 749 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env bash
  2. # http://c2.com/doc/expense/
  3. if (( $# == 0 )); then
  4. printf "Usage: %s <file>" $(basename "$0")
  5. exit 1
  6. fi
  7. file="$1"
  8. if [ ! -r "$file" ]; then
  9. printf "Unable to read fie: '%s'" "$file"
  10. exit 1
  11. fi
  12. exec awk '
  13. $1 ~ /^[A-Z]+[A-Z0-9]*$/ {
  14. if (sums[$1] == "" || $1 == "SUM") {
  15. sums[$1] = sum # Define Symbol
  16. $1 = sum
  17. sum = 0
  18. }
  19. else {
  20. $1 = sums[$1] # Dereference Symbol
  21. }
  22. }
  23. ($1+0) != 0 {$1 = sprintf("%7.2f", $1)} # Pretty Print
  24. {print}
  25. $2 == "*" {$1 *= $3} # Explicit Calculations
  26. $2 == "/" {$1 /= $3}
  27. $2 == "DB" {$1 = -$1}
  28. $2 == "CR" {$1 = -$1}
  29. NF == 0 {sum = 0} # Implicit Summation
  30. {sum += $1}
  31. ' "$@"