11

I have to read log file between and my question is how to get only the text in the file wich is between two dates using unix commands? Every line contain date and I want to get the text in the file which dates are equal or bigger that the FROM date and lower or equal than the TO date.
The format of the date is 13/05/23 01:58.
The second date is for an example 13/05/13 07:50. Example

13:41:55,110  INFO HellowordlsThis text is generated by me
3
  • Is the date at the beginning of the line? How is the line formatted?
    – Chris Down
    May 23, 2013 at 11:33
  • The date is at the begining of the line. Somewhere there are only not full date(hh:mm:ss). I've updated the question body. Please see the example. May 23, 2013 at 11:39
  • Everything you want is already in this post: stackoverflow.com/questions/7706095/…
    – slm
    May 23, 2013 at 11:46

1 Answer 1

19

Since the dates are in a format where the chronological order is the same as the lexical order, you can do:

awk '$0 >= "13/05/13 07:50" && $0 <= "13/05/23 01:58"'

(assuming the timestamps are at the beginning of each line)

3
  • 3
    You can also use awk's range patterns: gnu.org/software/gawk/manual/html_node/Ranges.html E.g awk '/^May 24 04/,/^May 24 05/' (for syslog format messages). This has slightly different semantics to Stephane's answer, but can be more compact.
    – CLF
    May 24, 2013 at 11:25
  • 2
    @CLF, and requires that the lines be in chronological order and that there be log lines on both those dates. May 24, 2013 at 11:47
  • what if you only know that the time ranges between 07:xx:xx to 01:xx:xx? How'd the regex change?
    – achhainsan
    Jul 18, 2023 at 4:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .