2

I have a log file. Each line is prepended with a date and the date format is as below:

2016-02-01 11:34:48,567.......

How do I grep this file for last 24 hours?

I tried few things such as below for specific time but this approach wont work for last X hours:

grep "2016-02-01 15:0[1-9]:00 logfile

Also, the following does work for -1hour but fails for -23 or -24hours:

 grep "^$(date -d -23hour +'%Y-%m-%d %H')" logfile
Nik_stack
  • 213
  • 2
  • 7
  • 17

1 Answers1

2

You can use sed if lines are arranged in chronological order.

sed -e "1,/^$(date -d -23hour +'%Y-%m-%d %H')/d"

This will delete all lines until the first match is found ie print all line after the first match till the end of file.

jijinp
  • 2,592
  • 1
  • 13
  • 15