#!/usr/bin/python # Copyright (C) 2012 Jeff Epler # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import sys import re p = {None: 0, 'k': 1, 'm': 2, 'g': 3, 't': 4, 'e': 5, 'z': 6, 'y': 7} rpd = ['', 'k', 'M', 'G', 'T', 'E', 'Z', 'Y'] rp2 = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Ei', 'Zi', 'Yi'] exp = {None: 1000, 'i': 1024} pat = re.compile("([-+]?[0-9.]+)(?:([kmgtezy])(i)?)?b?") prefix_seen = False def nonzero((v,p,e)): return bool(v) def val(s): global prefix_seen m = pat.match(s.lower()) if m.group(2): prefix_seen = True return float(m.group(1)), p[m.group(2)], exp[m.group(3)] def sub(a, b): av, ap, ae = a bv, bp, be = b if ap > bp: v = av - bv * be ** (bp-ap) p = ap e = ae else: v = av * ae ** (ap-bp) - bv p = bp e = be while v and abs(v) > e: v /= e p += 1 while v and abs(v) < (1./e): v *= e p -= 1 return (v, p, e) def show((v,p,e)): if not prefix_seen: return "%d" % int(v * e ** p) s = "%+.2f" % v s = s.rstrip("0").rstrip(".") if e == 1024: return "%s%s" % (s, rp2[p]) else: return "%s%s" % (s, rpd[p]) def read_du(f): if f == "-": f = sys.stdin else: f = open(f) d = {} for line in f: v, k = line.strip().split(None, 1) d[k] = val(v) return d d1 = read_du(sys.argv[1]) if len(sys.argv) > 2: d2 = read_du(sys.argv[2]) else: d2 = read_du('-') while d1: k, v1 = d1.popitem() if k in d2: v2 = d2.pop(k) else: v2 = val("0") dv = sub(v2, v1) if nonzero(dv): print "%s\t%s" % (show(dv), k) while d2: k, v2 = d2.popitem() if nonzero(v2): print "%s\t%s" % (show(v2), k)