Linux – Unix Command to grep a time range


I have a log file, saved with particular date. I wanna fetch log entries during a particular time and date range to another file.

Ex: all entries from
2014-12-04 00:00:00 time to 2014-12-04 17:00:00

Best Answer

Try egrep:

pttrn="2014-12-04 0[0-9]"
pttrn="${pttrn}|2014-12-04 1[0-6]"
pttrn="${pttrn}|2014-12-04 17:00:00"

egrep "${pttrn}" <logfile>

The egrep pattern contains three parts. The first part grabs everything from 00:00:00 to 09:59:59. The second part grabs everything from 10:00:00 to 16:59:59, and the third part grabs 17:00:00.