Sort
如何按浮點數對行進行排序
我有這樣一個文件:
name: xxx --- time: 5.4 seconds name: yyy --- time: 3.2 seconds name: zzz --- time: 6.4 seconds ...
現在我想通過這些浮點數對這個文件進行排序以生成一個新文件,如下所示:
name: yyy --- time: 3.2 seconds name: xxx --- time: 5.4 seconds name: zzz --- time: 6.4 seconds ...
我已經嘗試過該命令
awk '{print $5}' myfile | sort -g
,但這只會顯示浮點數。
如果使用 GNU
sort
或兼容的,您可以使用其-g
開關進行一般數字排序:$ sort -g -k5,5 file name: yyy --- time: 3.2 seconds name: xxx --- time: 5.4 seconds name: zzz --- time: 6.4 seconds
-k5,5
告訴 sort 只對第 5 列執行排序。用法
請記住
info sort
頁面中的詳細資訊:'--general-numeric-sort' '--sort=general-numeric' Sort numerically, converting a prefix of each line to a long double-precision floating point number. *Note Floating point::. Do not report overflow, underflow, or conversion errors. Use the following collating sequence: * Lines that do not start with numbers (all considered to be equal). * NaNs ("Not a Number" values, in IEEE floating point arithmetic) in a consistent but machine-dependent order. * Minus infinity. * Finite numbers in ascending numeric order (with -0 and +0 equal). * Plus infinity. Use this option only if there is no alternative; it is much slower than '--numeric-sort' ('-n') and it can lose information when converting to floating point.