Skip to content

Instantly share code, notes, and snippets.

@kwbr
Forked from samask/awk-expense-calculator.awk
Created May 2, 2021 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwbr/e29a3d9c8cd4b8c4bcb9ebbfd428d553 to your computer and use it in GitHub Desktop.
Save kwbr/e29a3d9c8cd4b8c4bcb9ebbfd428d553 to your computer and use it in GitHub Desktop.
Awk Expense Calculator, by Ward Cunningham
#!/usr/bin/awk -f
# Source: http://c2.com/doc/expense/
/^[A-Z]+[A-Z0-9]*$/ {
if (sums[$1] == "" || $1 == "SUM") {
sums[$1] = sum # Define Symbol
$1 = sum
sum = 0
}
else {
$1 = sums[$1] # Dereference Symbol
}
}
($1+0) != 0 {$1 = sprintf("%7.2f", $1)} # Pretty Print
{print}
$2 == "*" {$1 *= $3} # Explicit Calculations
$2 == "/" {$1 /= $3}
$2 == "DB" {$1 = -$1}
$2 == "CR" {$1 = -$1}
NF == 0 {sum = 0} # Implicit Summation
{sum += $1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment