awk | Extract Log on the basis of Time

202 views Asked by At

I have server log in the following format and I want awk which can extract logs between 2 dates.

Log Format:

00:00:00,002 INFO [LOG.XXX] XXX
01:11:00,001 INFO [LOG.XXX] XXX
02:00:01,002 INFO [LOG.XXX] XXX
SOME JUNK
02:02:00,002 INFO [LOG.XXX] XXX
03:11:00,001 INFO [LOG.XXX] XXX
SOME JUNK
03:00:00,002 INFO [LOG.XXX] XXX
04:00:00,001 INFO [LOG.XXX] XXX
10:00:01,002 INFO [LOG.XXX] XXX
10:59:01,002 INFO [LOG.XXX] XXX
12:03:01,002 INFO [LOG.XXX] XXX

Here, I want to fetch logs between Time: 01 to 10

Output

01:11:00,001 INFO [LOG.XXX] XXX
02:00:01,002 INFO [LOG.XXX] XXX
SOME JUNK
02:02:00,002 INFO [LOG.XXX] XXX
03:11:00,001 INFO [LOG.XXX] XXX
SOME JUNK
03:00:00,002 INFO [LOG.XXX] XXX
04:00:00,001 INFO [LOG.XXX] XXX
10:00:01,002 INFO [LOG.XXX] XXX
10:59:01,002 INFO [LOG.XXX] XXX

Options That I tried:
I can grep the time stamp with "-n" option and then cut those line number with sed command. But Issue with this approach is my log file is in GB and this makes it very slow, so I want awk to serve this purpose.

2

There are 2 answers

1
cnicutar On BEST ANSWER

You can do it with awk

$ cat f | awk '/^01:11/,/^10:59/'
01:11:00,001 INFO [LOG.XXX] XXX
02:00:01,002 INFO [LOG.XXX] XXX
SOME JUNK
02:02:00,002 INFO [LOG.XXX] XXX
03:11:00,001 INFO [LOG.XXX] XXX
SOME JUNK
03:00:00,002 INFO [LOG.XXX] XXX
04:00:00,001 INFO [LOG.XXX] XXX
10:00:01,002 INFO [LOG.XXX] XXX
10:59:01,002 INFO [LOG.XXX] XXX
0
Rakholiya Jenish On

You can also try:

for i in `seq $start_time $end_time`; do #$start_time and $end_time are the starting and ending time of the log that you want.
    i=`printf %.2d $i`;
    awk "/^$i:..:../" file_name;
done

Assuming SOME JUNK in your input also start with some time stamp.