Skip to content

Instantly share code, notes, and snippets.

@mjf
Created January 13, 2012 08:36
Show Gist options
  • Save mjf/1605100 to your computer and use it in GitHub Desktop.
Save mjf/1605100 to your computer and use it in GitHub Desktop.
DUSUM - Summarize Output of Program du(1) with human-readable mode auto-detection
#! /usr/bin/awk -f
# DUSUM - Summarize Output of Program du(1)
# Copyright (C) 2012 Matous J. Fialka, <http://mjf.cz/>
# Released under the terms of The MIT License
#
# Note: Human-readable mode of du(1) is auto-detected.
BEGIN {
B = 1
K = 1024 * B
M = 1024 * K
G = 1024 * M
T = 1024 * G
S = 0
H = 0
}
$1 ~ /^[0-9]+[KMGT]/ {
H = 1
}
$1 ~ /^[0-9]+/ {
sub(/B$/, $1)
S += $1 * B
}
$1 ~ /^[0-9]+K/ {
sub(/K$/, $1)
S += $1 * K
}
$1 ~ /^[0-9]+M/ {
sub(/M$/, $1)
S += $1 * M
}
$1 ~ /^[0-9]+G/ {
sub(/G$/, $1)
S += $1 * G
}
$1 ~ /^[0-9]+T/ {
sub(/T$/, $1)
S += $1 * T
}
END {
if(H) {
if(S < K)
printf("%dB", S / B)
else if(S < M)
printf("%dK", S / K)
else if(S < G)
printf("%dM", S / M)
else if(S < T)
printf("%dG", S / G)
else
printf("%dT", S / T)
printf("\n")
} else
printf("%d\n", S / B)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment