Mercurial > hg > Members > anatofuz > Perl6_log_analyze_example
changeset 1:4f7103163762 default tip
update
author | Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 18 Jul 2018 18:03:58 +0900 |
parents | b42d5cbbd972 |
children | |
files | log_analyze.py log_analyze.rb |
diffstat | 2 files changed, 27 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/log_analyze.py Wed Jul 18 18:03:58 2018 +0900 @@ -0,0 +1,26 @@ +#!/usr/bin/env python +import re +import sys +from collections import defaultdict + +file_path = "/var/log/system.log" +args = sys.argv + +if args == 3: + if args[1] == "-f": + file_path = args[2] + +count = defaultdict(int) + +with open(file_path) as f: + for line in f: + match = re.search(r'\w+ \d{0,2} (?:\d{2}:?){3} (?:anatofuzMBP|anatofuz-15) ([\w.]+)\[\d+\]',line) + if match: + count[match.group(1)]+=1 + +total = 0 + +for key in count.keys(): + total +=count[key] + +print(total)
--- a/log_analyze.rb Wed Jul 18 17:25:20 2018 +0900 +++ b/log_analyze.rb Wed Jul 18 18:03:58 2018 +0900 @@ -7,7 +7,7 @@ File.open(file,'r') do |f| f.each_line do |line| - if line =~ /\w \d{0,2} (?:\d{2}:?){3} #{user_name} ([\w.]+)\[\d+\]/ + if line =~ /\w+ \d{0,2} (?:\d{2}:?){3} #{user_name} ([\w.]+)\[\d+\]/ count[$1] += 1 end end