Awk is used to create additional columns for the sort keys. He only calls du once. The result should look exactly like du .
I split it into several lines, but it can be combined into a single line.
du -h | awk '{printf "%s %08.2f\t%s\n", index("KMG", substr($1, length($1))), substr($1, 0, length($1)-1), $0}' | sort -r | cut -f2,3
Explanation:
- BEGIN - create a row for indexing to replace 1, 2, 3 for K, M, G to group by units, if there is no unit (size less than 1K), then there is no match and zero is returned (excellent!)
- print new fields - unit, value (to make alpha sorting work with zero, fixed length) and the original line
- index of the last character of the size field
- pull out the digital part of the size
- sort results, discard additional columns
Try without the cut to see what it does.
Edit:
Here's the version that does the sorting in the AWK script and doesn't need a cut:
du -h | awk '{idx = sprintf("%s %08.2f %s", index("KMG", substr($1, length($1))), substr($1, 0, length($1)-1), $0); lines[idx] = $0} END {c = asorti(lines, sorted); for (i = c; i >= 1; i--) print lines[sorted[i]]}'
Dennis williamson
source share