Drinking coffee with AWK

Keep track of what your office mates owe for the coffee they drink with a simple AWK program.
188 readers like this.
Getting started with Perlbrew

freephotocc via Pixabay CC0

The following is based on a true story, although some names and details have been changed.

A long time ago, in a place far away, there was an office. The office did not, for various reasons, buy instant coffee. Some workers in that office got together and decided to institute the "Coffee Corner."

A member of the Coffee Corner would buy some instant coffee, and the other members would pay them back. It came to pass that some people drank more coffee than others, so the level of a "half-member" was added: a half-member was allowed a limited number of coffees per week and would pay half of what a member paid.

Managing this was a huge pain. I had just read The Unix Programming Environment and wanted to practice my AWK programming. So I volunteered to create a system.

Step 1: I kept a database of members and their debt to the Coffee Corner. I did it in an AWK-friendly format, where fields are separated by colons:

member:john:1:22
member:jane:0.5:33
member:pratyush:0.5:17
member:jing:1:27

The first field above identifies what kind of row this is (member). The second field is the member's name (i.e., their email username without the @). The next field is their membership level (full=1 or half=0.5). The last field is their debt to the Coffee Corner. A positive number means they owe money, a negative number means the Coffee Corner owes them.

Step 2: I kept a log of inputs to and outputs from the Coffee Corner:

payment:jane:33
payment:pratyush:17
bought:john:60
payback:john:50

Jane paid $33, Pratyush paid $17, John bought $60 worth of coffee, and the Coffee Corner paid John $50.

Step 3: I was ready to write some code. The code would process the members and payments and spit out an updated members file with the new debts.

#!/usr/bin/env --split-string=awk -F: -f

The shebang (#!) line required some work! I used the env command to allow passing multiple arguments from the shebang: specifically, the -F command-line argument to AWK tells it what the field separator is.

An AWK program is a sequence of rules. (It can also contain function definitions, but I don't need any for the Coffee Corner.)

The first rule reads the members file. When I run the command, I always give it the members file first, and the payments file second. It uses AWK associative arrays to record membership levels in the members array and current debt in the debt array.

$1 == "member" {
   members[$2]=$3
   debt[$2]=$4
   total_members += $3
}

The second rule reduces the debt when a payment is recorded.

$1 == "payment" {
   debt[$2] -= $3
}

Payback is the opposite: it increases the debt. This elegantly supports the case of accidentally giving someone too much money.

$1 == "payback" {
   debt[$2] += $3
}

The most complicated part happens when someone buys ("bought") instant coffee for the Coffee Club's use. It is treated as a payment and the person's debt is reduced by the appropriate amount. Next, it calculates the per-member fee. It iterates over all members and increases their debt, according to their level of membership.

$1 == "bought" {
   debt[$2] -= $3
   per_member = $3/total_members
   for (x in members) {
       debt[x] += per_member * members[x]
   }
}

The END pattern is special: it happens exactly once, when AWK has no more lines to process. At this point, it spits out the new members file with updated debt levels.

END {
   for (x in members) {
       printf "%s:%s:%s\n", x, members[x], debt[x]
   }
}

Along with a script that iterates over the members and sends a reminder email to people to pay their dues (for positive debts), this system managed the Coffee Corner for quite a while.

Moshe sitting down, head slightly to the side. His t-shirt has Guardians of the Galaxy silhoutes against a background of sound visualization bars.
Moshe has been involved in the Linux community since 1998, helping in Linux "installation parties". He has been programming Python since 1999, and has contributed to the core Python interpreter. Moshe has been a DevOps/SRE since before those terms existed, caring deeply about software reliability, build reproducibility and other such things.

8 Comments

Nice, back to basic :-)

Perfect example of over-engineering. Would have been easier to just have everyone chip in $5 each week and divy up anything left over now and then.

But then, what fun would that have been. :D

Yay! for kewl code to do simple cost tracking.
Yuck! for instant coffee (no wonder you only have 4 members...) ?

thanks for sharing,is wonderful.

Isn't this a lot of what is wrong with office culture?

Couldn't get it to work at all with the shebang line. Worked fine with:
awk -F":" -f proc.awk members payments

as long as I put the code without shebang in the proc.awk file with a closing brace... The env command didn't like anything I tried. Tried exec, tried bash with awk quoted, got tired of making coffee...

Nice^)

well, this is ... awk ward ;)

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.