Command to grep the service with in a timeframe

Guys,

I am trying to use this command to find out the occurrence of the service "Loadservice" from the log file "log.06102010.svr1" in between the time frame 02:00:00 to 03:00:00 on the day 06-10-2010.

sed -n '/2010-06-10 02:00:00/,/2010-06-10 03:00:00/p' | fgrep "Loadservice" log.06102013.svr1 | wc -w

I just need to find the wordcount of Loadservice and I am getting incorrect outcome from the above command. Any help will be really appreciated.

Regards,
Kris

sed doesn't work that way. If you don't have a line EXACTLY matching 2010-06-10 02:00:00, it will never begin, and if you don't have a line EXACTLY matching 2010-06-10 03:00:00 it will never end.

Also, word count probably isn't what you're looking for, but the number of lines containing that word.

You can probably do it with awk, but we will need to see what your logfile looks like.

1 Like

I need the number of words of 'Loadservice' between 2-3 AM

Show a line with 'loadservice' in it and how you expect it to be counted.

1 Like

The first command in your pipe should open the file; the second command should default to its stdin (that reads from the pipe).
But perhaps can be boiled down to one sed command

sed -n '/2010-06-10 02:/,/2010-06-10 03:/{/Loadservice/p;}' log.06102013.svr1

Still it needs to find at least one 02: and one 03: entry in the file.
It is identical with

sed '/2010-06-10 02:/,/2010-06-10 03:/!d;/Loadservice/!d' log.06102013.svr1
1 Like

Thanks MadeinGermany.

Tats works perfectly fine and if you add grep Loadservice | wc -l at the end it gives me the perfect count.

sed '/2010-06-10 02:/,/2010-06-10 03:/!d;/Loadservice/!d' log.06102013.svr1 |grep Loadservice | wc -l

---------- Post updated at 09:28 AM ---------- Previous update was at 09:26 AM ----------

I need the count for every hour, so I am trying to put this in a script which gives me the output for every single hour from 00:00 to 23:00

Please show a line with 'loadservice' in it and how you expect it to be counted.

Given your log file has date and time in the first two fields of every entry, try:

awk '/^2010-06-10.*Loadservice/ {CNT[substr($2, 1, 2)]++} END {for (c in CNT) print c, CNT [c]}' log.06102013.svr1

Corona,

Here is the file info:

2010-06-10 00:00:35,222|B|stack|sys|||gprs|true|1|{http:///}LoadService|info
2010-06-10 00:00:35,333|B|stack|sys|||gprs|true|9|{http:///}LoadService|info
2010-06-10 01:00:00,333|B|stack|sys|||gprs|true|2|{http:///}LoadService|info

when I execute the script then it gives me '3'

The first time in the entire thread "LoadService" has an upper case "S". That built in,

awk '/^2010-06-10.*LoadService/ {CNT[substr($2, 1, 2)]++} END {for (c in CNT) print c, CNT [c]}' file
00 2
01 1
1 Like

Thanks Rudy.
I am not that good at scripting, so can you explain me the script and out come?

00 2
01 1

I am getting something like this when I execute the awk command. so, does 00 means hours and 2 is the count?

Yes, that's the hour and its count.
The awk looks for lines that contain the target date AND the search word "LoadService". If found, a counter array indexed by the hour (two digits from time string) is incremented. In the end, the result is printed.

The nice thing about dates in YYYY-MM-DD HH:MM:SS order is they compare alphabetically. An exact match is no longer necessary -- you can use >, >=, <, <= to compare the date and get logical answers. awk will take one or more files, too.

$ awk -F"|" '{ sub(/,.*/, "", $1) } ; ($1 >= S) && ($1 <= E) && /LoadService/ { L++ } END { print L+0 }' S="2010-06-10 00:00:00" E="2010-06-10 01:00:00" inputfile1 inputfile2

3

$

Thanks much Corona

If each LoadService line is prefixed with the time, you can simply run

grep '^2010-06-10 02:.*LoadService'

on the logfile to get the entries between 02:00:00 and 02:59:59
and

grep -c ... 

gives the number of matches.