LoginSignup
0
1

More than 1 year has passed since last update.

ログの件数を1秒毎に集計する

Last updated at Posted at 2022-09-23

活用する場面

  • 秒単位で件数を集計したい時
    • 1秒間に何件のメール送信が成功しているのか等

前提

  • 以下のようなログがあるとする
    • VSCordなどで整形して以下の形式に近づけても良い
Desktop/log/sample.log
2022-07-01 11:42:47:384 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167926
2022-07-01 11:42:47:478 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167927
2022-07-01 11:42:47:678 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167929
2022-07-01 11:42:47:775 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167930
2022-07-01 11:42:47:878 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167931
2022-07-01 11:42:47:974 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167932
2022-07-01 11:42:48:080 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167933
2022-07-01 11:42:48:170 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167934
2022-07-01 11:42:48:265 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167935
2022-07-01 11:42:48:368 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167936
2022-07-01 11:42:48:459 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167937
2022-07-01 11:42:48:555 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167938
2022-07-01 11:42:48:649 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167939
2022-07-01 11:42:48:744 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167940
2022-07-01 11:42:48:841 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167941
2022-07-01 11:42:49:058 sample-batch BATCH sample  INFO SUCCESS : Gmailの送信に成功しました。Gmailの通知ID: 10167943

結論

$ cat 集計したいファイルのパス | cut -f2 -d' ' | cut -f1,2,3 -d':' | uniq -c
(base) sample@SampleMBP ~ % cd ~/Desktop/log
(base) sample@SampleMBP log % cat /Users/username/Desktop/log/sample.log | cut -f2 -d' ' | cut -f1,2,3 -d':' | uniq -c
   6 11:42:47
   9 11:42:48
   1 11:42:49

🎉🎉ログの件数が1秒毎に集計されている🎉🎉

ログの数が多ければ多いほど有効な小技

詳細①

$ cat 集計したいファイルのパス | cut -f2 -d' '
terminal
(base) sample@SampleMBP log % cat /Users/username/Desktop/log/sample.log | cut -f2 -d' '
11:42:47:384
11:42:47:478
11:42:47:678
11:42:47:775
11:42:47:878
11:42:47:974
11:42:48:080
11:42:48:170
11:42:48:265
11:42:48:368
11:42:48:459
11:42:48:555
11:42:48:649
11:42:48:744
11:42:48:841
11:42:49:058

詳細②

$ cat 集計したいファイルのパス | cut -f2 -d' ' | cut -f1,2,3 -d':'
terminal
(base) sample@SampleMBP log % cat /Users/username/Desktop/log/sample.log | cut -f2 -d' ' | cut -f1,2,3 -d':'
11:42:47
11:42:47
11:42:47
11:42:47
11:42:47
11:42:47
11:42:48
11:42:48
11:42:48
11:42:48
11:42:48
11:42:48
11:42:48
11:42:48
11:42:48
11:42:49

詳細③

$ cat 集計したいファイルのパス | cut -f2 -d' ' | cut -f1,2,3 -d':' | uniq -c
terminal
(base) sample@SampleMBP log % cat /Users/username/Desktop/log/sample.log | cut -f2 -d' ' | cut -f1,2,3 -d':' | uniq -c
   6 11:42:47
   9 11:42:48
   1 11:42:49

  • この記事と組み合わせてファイルに出力するのもあり

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1