"sort -h"が正しく機能しない

このファイルを並べ替えようとしています。

1000000911M
1G
2G
1.5G
100M

しかし、これを使用すると、次のようなsort -h file結果が得られます。

100M
1000000911M
1G
1.5G
2G

ソートに問題はありませんか?それでは、M、Gを明示的に拡張することなく、どのようにこれを防ぐことができますか?

ベストアンサー1

これはマニュアルページに文書化されていますsort

-h, --human-numeric-sort, --sort=human-numeric
         Sort by numerical value, but take into account the SI suffix, if present.
         Sort first by numeric sign (negative, zero, or positive); then by SI
         suffix (either empty, or `k' or `K', or one of `MGTPEZY', in that order);
         and finally by numeric value.  The SI suffix must immediately follow the
         number.  For example, '12345K' sorts before '1M', because M is "larger"
         than K.  This sort option is useful for sorting the output of a single
         invocation of 'df' command with -h or -H options (human-readable).

関連部分は4番目の文です。

For example, '12345K' sorts before '1M', because M is "larger" than K.

これはあなたの1000000911M1Gとその背後にある線2Gの間で観察されるものです。

通常、これらのサフィックスを含む出力を生成するソフトウェアは、多くの有効数字を出力する代わりにサフィックスを切り替えます。

おすすめ記事