【Splunk】sortはフィールドをソートする

sortコマンドの説明を書きます。
以下の記事の派生記事です。
canada-lemon.hatenablog.com

sortはフィールドをソートするコマンドです。

構文例
| sort <ソートしたいフィールド名>

使用例1:生徒の点数を昇順にソートする

sortを使って生徒の点数をソートしてみます。

以下のSPLを実行してダミーデータを用意します。
4人の生徒の英数国の点数です。
makeresultsってなんぞや?という方は以下の記事をご参照ください。
canada-lemon.hatenablog.com

| makeresults format=csv 
data="Class,Name,English,Math,Japanese
2年1組,Alfred,90,60,70
2年1組,Gascoigne,80,80,80
2年2組,Gehrman,60,100,50
2年2組,Ludwig,70,70,90"
| table Class Name English Math Japanese
Class Name English Math Japanese
2年1組 Alfred 90 60 70
2年1組 Gascoigne 80 80 80
2年2組 Gehrman 60 100 50
2年2組 Ludwig 70 70 90



英語の点数で昇順にソートしてみます。

| makeresults format=csv 
data="Class,Name,English,Math,Japanese
2年1組,Alfred,90,60,70
2年1組,Gascoigne,80,80,80
2年2組,Gehrman,60,100,50
2年2組,Ludwig,70,70,90"
| table Class Name English Math Japanese
| sort English
Class Name English Math Japanese
2年2組 Gehrman 60 100 50
2年2組 Ludwig 70 70 90
2年1組 Gascoigne 80 80 80
2年1組 Alfred 90 60 70

フィールド名の前に-をつけると降順になります。
また、| sort <フィールドA> <フィールドB>と書くと、フィールドAでソートした後にフィールドBでソートします。

備考1:ソートには件数制限がある

ソート機能はデフォルトだと1万行が上限となっており、上限を超過すると結果が切り捨てられます。
| sort 0 Englishのようにフィールドの前に0を書くと上限を撤廃できますが、パフォーマンスが悪くなる可能性があります。
(そもそもパフォーマンスが悪くなるから上限を設けてるわけですしね)

こちらの公式ドキュメントもご参照ください。

Specify the number of results to return from the sorted results. If no count is specified, the default limit of 10000 is used. If 0 is specified, all results are returned. You can specify the count using an integer or precede the count with a label, for example limit=10. Using sort 0 might have a negative impact performance, depending on how many results are returned.

docs.splunk.com

本記事ではsortコマンドの説明をしました。
ここまで読んでいただき、ありがとうございました。

Splunk頻出SPLシリーズ