
ログファイルを表示し、特定のミリ秒/秒数を grep するスクリプトを作成しました。最小値と最大値を表示するものを作成しましたが、リストの平均を見つける必要があります。
logdir_8080='/opt/product/apachetomcat/8.5.5_8080/logs'
cd "$logdir_8080"
get_file=`ls -rt localhost_access_log.*.txt|tail -1`
cat $get_file | tail -1000 | grep "objectId" | awk -F 'HTTP/1.1" 200' '{
print $2}'|awk -F' ' '{ print $2 }' | sort - n>/opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt
最小値:
cat /opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt|head -1
最大値:
cat /opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt|tail -1
リストのアイデア:
233
249
283
283
302
303
332
333
643
851
965
965
972
1022
1135
1182
1213
1232
1264
1273
1390
1403
1414
1429
1474
1537
1540
1543
1545
1556
1565
1566
1577
1589
1591
1599
1602
1621
1622
1647
1653
1705
1740
1772
1774
1933
1935
1983
1990
平均を取得するにはどうすればいいですか?
答え1
ファイル内にあるのが数字だけであれば、awk コマンドを 1 つ使用してすべてを取得できます。
awk 'BEGIN {themin=10000000; themax=0; thecount=0; thesum=0}
{for (i=1; i<=NF; i++) {
thesum += $i;
thecount++;
if ($i < themin) {themin = $i}
if ($i > themax) {themax = $i}
}}
END {
printf("The min is %d\nThe max is %d\nThe sum is %d\nThe total number of items is %d\nThe average of those items is %d\n", themin, themax, thesum, thecount, int(thesum/thecount))
}' _file_
さまざまなセクションが見えるように分割しました。
BEGIN は、使用するすべての変数を初期化するだけです。変数 themin は、ファイル内のどの値よりも大きい限り、任意の大きな値に設定できます。
中央部分は、各行とその行の各フィールドをループするだけです。数値を合計し、すべての数値をカウントして、最後に単純な除算を実行できるようにします。2 つの if ステートメントは、最小値と最大値を収集します。
END は、最小値、最大値、平均値など、関連するすべての情報を出力します。
お役に立てれば。
答え2
すでに素晴らしい解決策がある問題に対して、独自の解決策を書く必要はありません。結局のところ、質問したのは StackOverflow ではなく SuperUser です。
例えばGNUを使うdatamash
$ datamash min 1 max 1 mean 1 < yourFile
233 1990 1272.0408163265
またはパッケージのツールを使用するnum-utils
$ numbound -l yourFile
233
$ numbound yourFile
1990
$ numaverage yourFile
1272.04081632653