Saturday, 15 September 2012

linux - Shell script to search for a string from a file and show number of instances per hour -



linux - Shell script to search for a string from a file and show number of instances per hour -

i want write shell script search string "/abcdefg/" , count hourly particular date printed timestamp in logs.

below sample log need take counts from:

2014-06-21 00:00:22,516: |[http://123.123.123.123:15123/abcdefg/ [200] [request_status,0;] [no-error] [134msec] 2014-06-21 00:00:22,531: |[http://123.123.123.123:15123/abcdefg/ [200] [request_status,0;] [no-error] [160msec] 2014-06-21 23:59:54,920: |[http://123.123.123.123:15123/abcdefg/ [200] [request_status,0;] [no-error] [149msec] 2014-06-21 23:59:54,923: |[http://123.123.123.123:15123/abcdefg/ [200] [request_status,0;] [no-error] [164msec]

below manual command can used count:

grep "/abcdefg/" abc.log |grep "2014-06-21 00" | grep "[200]"|wc -l grep "/abcdefg/" abc.log |grep "2014-06-21 01" | grep "[200]"|wc -l grep "/abcdefg/" abc.log |grep "2014-06-21 03" | grep "[200]"|wc -l etc

this might work you:

awk '$1 ~ /2014-06-21/ && $3 ~ /abcdefg/ && $4 == "[200]" { ++cnts[int(substr($2, 1, 2))] } end { for(i = 0; < 24; ++i) printf("%02d: %4d\n", i, in cnts ? cnts[i] : 0); } ' logfile

you create shell script. next takes 1 3 parameters. if sec isn't supplied, defaults "abcdefg"; if 3rd isn't supplied, defaults "logfile". (obviously can alter defaults.)

#!/bin/sh awk -vdate="$1" -vstr="${2:-abcdefg}" ' $1 ~ date && $3 ~ str && $4 == "[200]" { ++cnts[int(substr($2, 1, 2))] } end { for(i=0; < 24; ++i) printf("%02d: %6d\n", i, in cnts ? cnts[i] : 0); } ' ${3:-logfile}

suppose it's called myscript. can phone call in next ways:

myscript 2014-06-21 myscript 2014-06-21 hijklmn myscript 2014-06-21 abcdefg anotherlogfile

linux string bash shell

No comments:

Post a Comment