Search code examples
pythonbashsedawkgrep

how to grep by date time range?


I have a log file looks like:

2011-03-21 00:01 xxxx
2011-03-22 04:05 xxxx
....
2011-03-25 10:12 xxxx
....
2011-04-04 12:23 xxxx

I want to have a script which requires 2 arguments as the date range, for example:

grep-date-range.sh 2011-03-25 2011-04-02

It will find all logs in [2011-03-25, 2011-04-02]. I know for a specific case i can use wildcard, but it's not general in my opinion. Could someone give me a solution?

EDIT: Python script is also acceptable.


Solution

  • his is a case where it may be better to write a short Python script. The high level date manipulations capabilities in thelanguage can be handy.

    The script bellow is very simple - with a bit more work it could take care of localtime differences, Daylight saving time, and so on.

    #! /usr/bin/python
    import sys
    from datetime import datetime
    d_format = "%Y-%m-%d"
    
    try:
        start = datetime.strptime(sys.argv[1], d_format)
        end = datetime.strptime(sys.argv[2], d_format) 
    except (TypeError, IndexError):
        sys.stderr.write("Example: grep-date-range.py 2011-03-25 2011-04-02 \n")
    
    for line in sys.stdin:
        try:
            date = datetime.strptime(line.split()[0], d_format)
            # suit the <=, <, comparisons bellow to your needs:
            if start <= date < end:
                sys.stdout.write(line)
        except (ValueError, IndexError):
            pass