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
  • Is the date at the beginning of the line? How is the line formatted? – Chris Down May 23 '13 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. – Jordan Borisov May 23 '13 at 11:39
  • Everything you want is already in this post: stackoverflow.com/questions/7706095/… – slm May 23 '13 at 11:46

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)

  • 2
    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 '13 at 11:25
  • 2
    @CLF, and requires that the lines be in chronological order and that there be log lines on both those dates. – Stéphane Chazelas May 24 '13 at 11:47

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.