I work with log files on a daily basis. I have been using sed to accomplish my goal of extracting certain lines of data between to time stamps. For example, I use:

sed '/20150720 15:06:00/,/20150720 16:25:00/! d' logfile.log > /tmp/logpart.log

However, This only works when the timestamps actually match a line in the file. How can I force sed to work on the data that I want without having to go into a file to get the actual time stamps? For instance, in the example above, I just want everything between 15:00 and 16:30, regardless of if there is a matching time stamp in the file.

  • Not quite the sed you're looking for, but an alternative – HBruijn Aug 2 '15 at 17:15
  • Please add a snippet of the logfile.log and sed fragments you have tried – 030 Aug 2 '15 at 21:09

As your log files have the good taste of having timestamps that are lexicographically sorted, you can simple compare strings using awk, eg:

awk 'substr($0,1,17)>="20150720 15:06:00" && substr($0,1,17)<="20150720 16:25:00"' <logfile.log

I've assumed here that your timestamp starts at column 1 (numbering from 1). If not, change the ,1, appropriately.

| improve this answer | |
  • 1
    Actually, I was able to just use wildcards on the time portions to accomplish what I was after, tested it and it worked. Something like this would work - sed '/20150630 02:20:*/,/20150630 13:40:*/! d' logfile.log > /tmp/logpart.log. This extracted all lines between 2:20 and 13:40. – user53029 Aug 3 '15 at 0:28
  • @user53029 Good idea. You dont actually need the * (which should be .*). Just shorten each pattern until you match a line. – meuh Aug 3 '15 at 5:56
  • suggestion: $1 " " $2 >= "20150720 15:06:00" (or any other field reference if not first word) instead of substr – NeronLeVelu Aug 7 '15 at 12: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.