This produces the following output:use strict; use warnings; my %scale = (K => 1024, M => 1024**2, G => 1024 ** 3); print map {$_->[1]} sort {$a->[0] <=> $b->[0]} map {/^(\d+)(\w)/; [$1 * $scale{$2}, $_];} <DATA>; __DATA__ 33G /data/A 37M /data/B 44G /data/C 46G /data/D 68G /data/E 114G /data/F 148G /data/G 169M /data/H 17K /data/I
Note that I have used powers-of-two for K, M and G unit prefixes, it is easy enough to change them to powers of 10 if one wants to comply with newer standards (it is even easier, because you can use a regex to add groups of three 0's), but I strongly suspect that the du utility still computes data volumes in power-of-two units.$ perl sort_du.pl 17K /data/I 37M /data/B 169M /data/H 33G /data/A 44G /data/C 46G /data/D 68G /data/E 114G /data/F 148G /data/G
Also note that I am not using the Schwartzian Transform for performance reasons, but only because I think it is very efficient "code-wise", just two instructions and 4 lines of actual code to solve the problem.
In reply to Re: sort file with your own logic
by Laurent_R
in thread sort file with your own logic
by citi2015
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |