Linux:sortコマンドで数値としてソートする方法

スポンサーリンク

sortコマンドで数値としてソートする方法

sortコマンドで数値としてソートするには-nオプションを指定します。

 

オプションの説明

    -n, --numeric-sort          compare according to string numerical value

 

以下のテキストをソートするとします。

$ cat test.txt
3
21
-10
100
0
-2
4

 

-nオプションを指定しない場合

$ sort test.txt
0
-10
100
-2
21
3
4

 

-nオプションを指定した場合

$ sort -n test.txt
-10
-2
0
3
4
21
100