# HG changeset patch # User Takahiro SHIMIZU # Date 1531902320 -32400 # Node ID b42d5cbbd972f9d82c8946c06916effefcf57b81 add diff -r 000000000000 -r b42d5cbbd972 java/build.gradle --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/build.gradle Wed Jul 18 17:25:20 2018 +0900 @@ -0,0 +1,26 @@ +plugins { + id 'java' +} + +group 'log_analyzer' +version '1.0-SNAPSHOT' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' +} + +compileJava { + options.compilerArgs << "-Werror" +} + +jar { + manifest { + attributes "Main-Class": "com.google.anatofuz.LogAnalyzer" + } +} diff -r 000000000000 -r b42d5cbbd972 java/settings.gradle --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/settings.gradle Wed Jul 18 17:25:20 2018 +0900 @@ -0,0 +1,2 @@ +rootProject.name = 'anatofuz' + diff -r 000000000000 -r b42d5cbbd972 java/src/main/java/com/google/anatofuz/LogAnalyzer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/src/main/java/com/google/anatofuz/LogAnalyzer.java Wed Jul 18 17:25:20 2018 +0900 @@ -0,0 +1,55 @@ +package com.google.anatofuz; + +import java.io.File; +import java.io.FileReader; +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.*; +import java.util.regex.Pattern; +import java.util.regex.Matcher; + +public class LogAnalyzer { + + public static void main(String args[]) { + + File file = new File("/var/log/system.log"); + + if (args.length != 0) { + if (args[0].equals("-f")) { + file = new File(args[1]); + } + } + + try { + FileReader filereader = new FileReader(file); + BufferedReader bufferedReader = new BufferedReader(filereader); + + String line; + Map map = new HashMap(0); + Pattern p = Pattern.compile("\\w+ \\d{0,2} (?:\\d{2}:?){3} (?:anatofuzMBP|anatofuz-15) ([\\w.]+)\\[\\d+\\]"); + + + while ((line = bufferedReader.readLine()) != null) { + Matcher matcher = p.matcher(line); + if (matcher.find()) { + map.merge(matcher.group(1),1,Integer::sum); + } + } + + int sum = 0; + + for (String key :map.keySet()){ + sum += map.get(key); + } + + System.out.println(sum); + + + } catch (FileNotFoundException ex){ + System.out.println(ex); + } catch (IOException ex){ + System.out.println(ex); + } + } +} diff -r 000000000000 -r b42d5cbbd972 log_analyze.p6 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/log_analyze.p6 Wed Jul 18 17:25:20 2018 +0900 @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +use v6; + +unit sub MAIN(:f($file) where { .IO.f } = '/var/log/system.log'); + +my $user_name = /'anatofuzMBP'|'anatofuz-15'/; +my $fh = open $file,:r; +my %count =(); + +for $fh.lines -> $line { + if ( $line ~~ /\w+ \s \d**0..3 \s [\d**2\:?]**3 \s $user_name \s (<[\w.]>+)\[\d+\]/) { + %count{$0}++; + } +} +$fh.close; +my $sum = 0; + +for %count.keys -> $key { + $sum += %count{$key}; +} + +$sum.say; diff -r 000000000000 -r b42d5cbbd972 log_analyze.pl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/log_analyze.pl Wed Jul 18 17:25:20 2018 +0900 @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +my $file = "/var/log/system.log"; + +if(@ARGV == 2){ + if ( $ARGV[0] eq "-f"){ + $file = $ARGV[1]; + } +} + +my $user_name = qr/anatofuzMBP|anatofuz-15/; +open my $fh, "<",$file; +my $count = {}; + +while (my $line = <$fh>) { + if ( $line =~ /\w \d{0,2} (?:\d{2}:?){3} $user_name ([\w.]+)\[\d+\]/){ + $count->{$1}++; + } +} + +my $sum = 0; + +for my $key (keys %$count){ + $sum += $count->{$key}; +} + +print "$sum\n"; diff -r 000000000000 -r b42d5cbbd972 log_analyze.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/log_analyze.rb Wed Jul 18 17:25:20 2018 +0900 @@ -0,0 +1,23 @@ +#!/usr/bin/env ruby + +file = "/var/log/system.log" + +user_name = Regexp.new("anatofuzMBP|anatofuz-15") +count = Hash.new(0) + +File.open(file,'r') do |f| + f.each_line do |line| + if line =~ /\w \d{0,2} (?:\d{2}:?){3} #{user_name} ([\w.]+)\[\d+\]/ + count[$1] += 1 + end + end +end + + +sum = 0 + +for key in count.keys + sum += count[key] +end + +p sum