Command-Line

“排序-h”無法正常工作

  • July 25, 2022

我嘗試對這個文件進行排序:

1000000911M
1G
2G
1.5G
100M

但使用sort -h file我得到:

100M
1000000911M
1G
1.5G
2G

排序有問題嗎?如果是這樣,如何避免這種情況而不必顯式擴展 M,G ?

這記錄在手冊頁中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).

相關部分是第四句:

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

這就是你在你的1000000911M線和1G它後面的線之間觀察到的2G

在通常的實踐中,使用這些後綴生成輸出的軟體會切換後綴,而不是輸出這麼多有效數字。

引用自:https://unix.stackexchange.com/questions/711159