特定の場所に基づいてファイルをソートしようとしても機能しません。ここにデータと出力があります。
~/scratch$ cat id_researchers_2018_sample
id - 884209 , researchers - 1
id - 896781 , researchers - 4
id - 901026 , researchers - 15
id - 904091 , researchers - 1
id - 905525 , researchers - 1
id - 908660 , researchers - 5
id - 908876 , researchers - 7
id - 910480 , researchers - 10
id - 916197 , researchers - 1
~/scratch$ sort -k 28,5 id_researchers_2018_sample
id - 884209 , researchers - 1
id - 896781 , researchers - 4
id - 901026 , researchers - 15
id - 904091 , researchers - 1
id - 905525 , researchers - 1
id - 908660 , researchers - 5
id - 908876 , researchers - 7
id - 910480 , researchers - 10
id - 916197 , researchers - 1
次のように、最後の列の数字に基づいてソートしたいと思います。
id - 884209 , researchers - 1
id - 904091 , researchers - 1
id - 905525 , researchers - 1
id - 916197 , researchers - 1
id - 896781 , researchers - 4
id - 908660 , researchers - 5
id - 908876 , researchers - 7
id - 910480 , researchers - 10
id - 901026 , researchers - 15
ベストアンサー1
7列に基づいて数字で並べ替えたいと思います。
これは、次のいずれかの方法で実行できます。
$ sort -n -k 7 file
id - 884209 , researchers - 1
id - 904091 , researchers - 1
id - 905525 , researchers - 1
id - 916197 , researchers - 1
id - 896781 , researchers - 4
id - 908660 , researchers - 5
id - 908876 , researchers - 7
id - 910480 , researchers - 10
id - 901026 , researchers - 15
または
$ sort -k 7n file
id - 884209 , researchers - 1
id - 904091 , researchers - 1
id - 905525 , researchers - 1
id - 916197 , researchers - 1
id - 896781 , researchers - 4
id - 908660 , researchers - 5
id - 908876 , researchers - 7
id - 910480 , researchers - 10
id - 901026 , researchers - 15
これは同じです。
この-n
オプションは、数値ソート(事前ソートとは反対)を指定します。上記の2番目の例では、n
列7に指定子/修飾子として追加します。
ソートキー列の指定は、列7から始まり、行を-k 7
ソートしますsort
(列7から行の終わりまで)。この場合、列7は最後なので、この列のみを表します。これが重要な場合は-k 7,7
(「from 7 to columns 7」)、代わりに使用できます。
2つのキーが等しいと比較されると、sort
行全体がソートキーとして使用されます。これが、例の最初の4行の結果を得る理由です。 2番目の列を2回並べ替えるには、sort -n -k 7,7 -k 2,2
またはsort -k 7,7n -k 2,2n
(各列の比較タイプを別々に指定)を使用できます。また7番ならそして2番目の列は、sort
行全体の事前編成比較を使用して、2行が等しいかどうかを比較します。
文字位置 29 (サンプル データの各行の末尾にある値の最初の数字に相当) を数値で並べ替えるには、次の手順を実行します。
$ sort -k 1.29n file
id - 884209 , researchers - 1
id - 904091 , researchers - 1
id - 905525 , researchers - 1
id - 916197 , researchers - 1
id - 896781 , researchers - 4
id - 908660 , researchers - 5
id - 908876 , researchers - 7
id - 910480 , researchers - 10
id - 901026 , researchers - 15
-k 1.29n
「与えられたキーでソート」を意味します。フィールド1の29番目の文字(前方、行末に)、図」。
上記のテキストで使用されている-k 7,7n
正確な値はです-k 7.1,7.1n
。