0

This question already has an answer here:

I am using the sed command to get the data between 2 dates. I am using regular expression matching the pattern for the dates.

The sample data I am using is somewhat like:

 192.168.0.123 - - [25/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [28/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [28/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [28/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [30/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [01/Dec/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [01/Dec/2018:13:25:09 +0530] "Data 4"

There are 2 problems I am facing.

  1. It is giving the output only till the first occurrence of the last date. i.e., if the command used is:

    $ sed -n '/26/Nov/2018/,/28/Nov/2018/ p' file.log

Then the output I am getting is:

 192.168.0.123 - - [25/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [28/Nov/2018:13:25:09 +0530] "Data 4"

But I am expecting

 192.168.0.123 - - [25/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [28/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [28/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [28/Nov/2018:13:25:53 +0530] "Data 6"
  1. If I am entering a date that does not exist in the log. I am getting all the data till the end of the file while I want it to be till the last place where data is present. i.e., if the command is:

    $ sed -n '/26/Nov/2018/,/29/Nov/2018/ p' file.log

Then the output is:

 192.168.0.123 - - [25/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [28/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [28/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [28/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [30/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [01/Dec/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [01/Dec/2018:13:25:09 +0530] "Data 4"

While I expect it to be:

 192.168.0.123 - - [25/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [28/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [28/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [28/Nov/2018:13:25:53 +0530] "Data 6"

marked as duplicate by Jeff Schaller, JigglyNaga, Stephen Harris, Kusalananda linux Dec 5 '18 at 12:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

I prefer to use dategrep to parse log based on date.

[root@server22 ~]# cat file.log 
 192.168.0.123 - - [25/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [28/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [28/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [28/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [30/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [01/Dec/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [01/Dec/2018:13:25:09 +0530] "Data 4"

to parse log between 26Nov to 29nov

[root@server22 ~]# dategrep --start 26/Nov/2018:00:00:00 --end 29/Nov/2018:00:00:00 file.log --format "%d/%b/%Y:%H:%M:%S"
 192.168.0.123 - - [26/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [26/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [28/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [28/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [28/Nov/2018:13:25:53 +0530] "Data 6"

Not the answer you're looking for? Browse other questions tagged or ask your own question.