ログファイルから時間ごとのデータをカウントする

ログファイルから時間ごとのデータをカウントする

ログ ファイルから 1 時間ごとのレコード数を取得したいと思います。サンプル データは次のとおりです。

001:2017-05-23 00:00:01 002:10.10.10.63
001:2017-05-23 00:00:03 002:10.10.10.63
001:2017-05-23 00:00:05 002:10.10.10.63
001:2017-05-23 00:00:07 002:10.10.10.63
001:2017-05-23 00:00:09 002:10.10.10.63
001:2017-05-23 01:00:12 002:10.10.10.63
001:2017-05-23 01:00:14 002:10.10.10.63

上記のデータ出力は次のようになります。

00 = 5
01 = 2

答え1

cut -f2 -d' ' logfile
| cut -f1 -d:
| sort
| uniq -c
| sed 's/ *\([0-9]\+\) \([0-9][0-9]\)/\2 = \1/'
| sort
  1. 時間のみを出力します。
  2. 時間のみを抽出します。
  3. 出力をソートする - 次の行で必要
  4. 各時間の発生回数をカウントし、count hour
  5. 書式を修正する
  6. 時間順に並び替え

答え2

find以下のように属性を使用できます-printf。私はこれを日常業務で使用しました。

find /path/ -type f -printf '%TY-%Tm-%Td-%TH\n' | sort | uniq -c

答え3

解決策はたくさんありますが、その1つは

log_file=/var/log/messages                        # log file for extract
d=2022-10-28                                      # start date
while [ "$d" != 2022-11-04 ]; do                  # loop for date range
  echo $d                                         # echo ACTUAL date
  for h in {00..24}; do                           # loop for hours
    act=$(date -d "$d" +'%b %d')" $h:"            # create date for ACTUAL date in requested format: %b return month in Jan Feb..., %d return month number
    echo $act                                     # Print actual hour
    grep "^$act" $log_file                        # grep $ACT from beginning of line of log_file and count lines
  done
  d=$(date -I -d "$d + 1 day")                    # add +1 day for start date to the main loop
done 

関連情報