#! /usr/bin/gawk -f # Last edited on 2014-01-21 15:38:17 by stolfilocal # Reads from stdin a raw transaction log obtained from http://bitcoinwisdom.com/. # Writes to stdout the same data reformatted as a nice table BEGIN \ { if (curr == "") {arg_error(("must define {curr}")); } if (ex == "") {arg_error(("must define {ex}")); } if (dt == "") {arg_error(("must define {dt}")); } printf "# Last edited on DATE TIME by USER\n" printf "# Exchange %s transaction log (price in %s).\n", ex, curr; printf "%20s ! %6s ! %s\n", "Timestamp", "Price", "V.BTC"; phase = "skipping"; curvolume = ""; } // \ { gsub(/1[.]7e[+]308/, "0.0"); gsub(/Infinity/, "0.0"); } /[^-+0-9.:]/ \ { # Non-numeric char if ((phase != "skipping") && (phase != "gotprice") && (phase != "finished")) { data_error(("invalid trans record")); } if (phase == "gotprice") { phase = "finished"; } next; } /^[ ]*$/ \ { # Blank line if ((phase != "skipping") && (phase != "gotprice") && (phase != "finished")) { data_error(("unexpected blank line")); } if (phase == "gotprice") { phase = "finished"; } next; } (NF == 2) \ { # next; if (phase != "skipping") { data_error(("invalid trans record")); } next; } (NF == 1) \ { if (phase == "finished") { # Should not occur data_error(("single-number line after trans log end")); } else if ((phase == "skipping") && (curvolume == "")) { # Current volume line: curvolume = $1; # Continue skipping. } else if ((phase == "skipping") || (phase == "gotprice")) { # Start of new trasaction volume = $1; if (volume !~ /^[0-9]+([.][0-9]+|)$/) { data_error(("bad volume " volume)); } phase = "gotvolume"; } else if (phase == "gotvolume") { tm = $1; if (tm !~ /^[0-9][0-9][:][0-9][0-9][:][0-9][0-9]$/) { data_error(("bad time " tm)); } phase = "gottime"; } else if (phase == "gottime") { price = $1; if (price !~ /^[0-9]+([.][0-9]+|)$/) { data_error(("bad price " price)); } printf "%s %s | %s | %s\n", dt, tm, price, volume; phase = "gotprice"; } else { data_error(("duh")); } next; } // \ { data_error(("bad format")); } END \ { if ((phase != "gotprice") && (phase != "finished")) { data_error(("file ended at phase" phase)); } } function data_error(msg) { printf "%s:%s: ** %s\n", FILENAME, FNR, msg > "/dev/stderr"; printf " «%s»\n", $0 > "/dev/stderr"; abort = 1; exit(abort); } function arg_error(msg) { printf "** %s\n", msg > "/dev/stderr"; abort = 1; exit(abort); }