0

I am trying to fetch data between a range of time intervals from the log file. However, the complete data is not fetched for the end date.

Below is the command used:

 sed -n '/26/Nov/2018/,/30/Nov/2018/ p' /path/file.log 

The data I am having in the above log file is somewhat like:

 192.168.0.123 - - [25/Nov/2018:13:24:59 +0530] "Data 1"
 192.168.0.123 - - [26/Nov/2018:13:24:59 +0530] "Data 2"
 192.168.0.123 - - [26/Nov/2018:13:25:09 +0530] "Data 3"
 192.168.0.123 - - [27/Nov/2018:13:25:09 +0530] "Data 4"
 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:53 +0530] "Data 7"
 192.168.0.123 - - [29/Nov/2018:13:25:53 +0530] "Data 8"
 192.168.0.123 - - [30/Nov/2018:13:25:53 +0530] "Data 9"
 192.168.0.123 - - [30/Nov/2018:13:25:53 +0530] "Data 10"
 192.168.0.123 - - [01/Dec/2018:13:25:53 +0530] "Data 11"
 192.168.0.123 - - [01/Dec/2018:13:25:53 +0530] "Data 12"
1
  • That should give you a syntax error. Please remember to always include any errors you get in your question. – terdon Dec 4 '18 at 15:19
2

That's because you are using / both as a separator for the sed match command and as part of the string being matched. You need to escape the internal /. Try this instead:

$  sed -n '/26\/Nov\/2018/,/30\/Nov\/2018/ p' file.log 
 192.168.0.123 - - [26/Nov/2018:13:24:59 +0530] "Data 2"
 192.168.0.123 - - [26/Nov/2018:13:25:09 +0530] "Data 3"
 192.168.0.123 - - [27/Nov/2018:13:25:09 +0530] "Data 4"
 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:53 +0530] "Data 7"
 192.168.0.123 - - [29/Nov/2018:13:25:53 +0530] "Data 8"
 192.168.0.123 - - [30/Nov/2018:13:25:53 +0530] "Data 9"
1
  • Hi @terdon. Sorry I had posted the question removing the backslashes. The problem I am facing is, if the end date is not matching with any date in the file, then it is fetching all the logs till the end of the file. Is there a way to stop it till the date before the date where the data is not present? – Anant Kumar Dec 5 '18 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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