balance.awk 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. BEGIN {
  2. phase = "DATE"
  3. }
  4. /;;/ {
  5. next
  6. }
  7. $1 ~ "-" || $1 ~ "/", $1 == "\n" {
  8. cor = $1
  9. if(cor ~ "/") {
  10. gsub("/", "-", cor);
  11. phase = "DATE"
  12. isDate = 0
  13. }
  14. if(cor ~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/) {
  15. if(total > 0.009 || total < -0.009) {
  16. print "aledger error, doesn't evaluate to zero. Line" NR, $0
  17. exit 1
  18. }
  19. isDate = 1;
  20. phase = "DATE"
  21. total = 0
  22. skip = 0;
  23. }
  24. if(!skip) {
  25. if(phase == "DATE") {
  26. if(isDate) {
  27. if(start != "" && $1 < start)
  28. skip = 1;
  29. if(end != "" && $1 > end)
  30. exit 0
  31. isDate = 0;
  32. entrytotal = 0;
  33. phase = "ENTRIES";
  34. } else {
  35. print "aledger error, failed to parse date at on line " NR ": " $0
  36. error = 1
  37. exit 1
  38. }
  39. } else if(phase == "ENTRIES") {
  40. isDate = 0;
  41. if($1 ~ ":") {
  42. sub("\\$", "", $2)
  43. entry[$1] += $2
  44. total += $2
  45. }
  46. }
  47. }
  48. }
  49. END {
  50. if(total > 0.009 || total < -0.009) {
  51. print "aledger error, doesn't evaluate to zero. Line " NR "," $0
  52. exit 1
  53. }
  54. if(!error) {
  55. for(i in entry) {
  56. printf("%s %.2f\n", i, entry[i])
  57. }
  58. }
  59. }