function sumItUp(units, cpu) { # avoid floating point arithmetic AT ALL COST! partsLen = split(cpu, parts, ".") if (partsLen == 1) { cost = parts[1] * 100 return cost * units } dec = parts[2] dec = substr(dec, 0, 2) for (i = length(dec); i < 2; i++) { dec = dec"0" } cost = parts[1] * 100 + dec return cost * units } BEGIN { RS="\n" FS=";" split(ARG_TICKER, _tmp_tickers, ";") for (i in _tmp_tickers) { tickers[_tmp_tickers[i]] = "" hasTickers = 1 } gsub(/[-: ]/, "", ARG_DATE_AFTER); gsub(/[-: ]/, "", ARG_DATE_BEFORE); } (hasTickers == 1) && ($2 in tickers == 0) { next } { fieldDate=$1 gsub(/[-: ]/, "", fieldDate); if (ARG_DATE_AFTER != "" && ARG_DATE_AFTER > fieldDate) { next } if (ARG_DATE_BEFORE != "" && ARG_DATE_BEFORE < fieldDate) { next } } { if ($3 == "buy") { profit[$2] += $6 } else if ($3 == "sell") { profit[$2] -= $6 } lastPrice[$2] = $4 } END { widestNum = 0 for (tic in profit) { profit[tic] = sumItUp(profit[tic], lastPrice[tic]) profit[tic] /= 100 str = sprintf("%.2f", profit[tic]) strLen = length(str) if (strLen > widestNum) { widestNum = strLen } } for (tic in profit) { printf "%-9s : %"widestNum".2f\n", tic, profit[tic] } }