Skip to content Skip to sidebar Skip to footer

Grep A Word And Find Its Count From Log File For Different Times

log file which contains following data. 2014-10-19 17:30:25: Creating destination directory: '\master1\users\jamesk\Java\chapter05\tech-support-complete\doc\' Loading source file

Solution 1:

import re
pattern=re.compile(r"\d{4}-\d{1,2}-\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}:|Error",re.IGNORECASE)
ll=pattern.findall(x)
d={}
for x in ll:
    if x!="Error":
        d[x]=0
        last=x
    else:
        d[last]=d[last]+1print d

Here x is your data or file.read().

Solution 2:

Simple job with Awk.

awk '/^[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9] [012][0-9]:[0-5][0-9]:[0-6][0-9]:/ {
       t=$0 }
    /Error/ { ++e[t] }
    END { for (s in e) print s "Error-Count=" e[s] }' logfile

Solution 3:

A direct awk:

awk '/^201[0-9].*:/{if (cont){print cont}cont=0;printf $0}/Error/{cont+=1}END{print cont}' infile

Explained code:

awk '/^201[0-9].*:/{ # Timestamp pattern reached
                    if (cont){
                              print cont # print previus timestamp
                             }           # counter if exists and not zero
                    cont=0 # initialize actual timestamp counter 
                    printf $0
                   } #  print timestamp WITHOUT linebreak
            /Error/{ # Error patter reached
                    cont+=1 # Aaccumulated count
                   }
     END{
        print cont # print remainder counter
        }' infile

Solution 4:

here you go using python:

>>>f = open('logfile').readlines()>>>i = 0>>>whileTrue:...if i+10 > len(f):...break...    tmp = len(re.findall('Error',"".join(f[i+1:i+10])))...print f[i].strip() + " Error-Count=" + str(tmp)...    i +=10... 
2014-10-19 17:30:25: Error-Count=6
2014-10-19 18:30:25: Error-Count=5
2014-10-19 19:30:25: Error-Count=4
2014-10-19 20:30:25: Error-Count=7

Post a Comment for "Grep A Word And Find Its Count From Log File For Different Times"