0

I'd like to extract all lines with between specific time, plus the filename attached with it.

Input example:

Dec  8 22:00:05 host kernel:
Dec  8 23:00:05 host kernel:
Dec  8 23:34:45 host kernel:
Dec  8 23:54:45 host kernel:
Dec  9 00:34:45 host kernel:
Dec  9 00:54:45 host kernel:
Dec  9 01:54:45 host kernel:
Dec  9 02:54:45 host kernel:
Dec  9 03:54:45 host kernel:
Dec  9 04:54:45 host kernel:
Dec  9 05:54:45 host kernel:

My Desired output :

Dec  8 23:00:05 host kernel:
Dec  8 23:34:45 host kernel:
Dec  8 23:54:45 host kernel:
Dec  9 00:34:45 host kernel:
Dec  9 00:54:45 host kernel:
2
  • Maybe you can adapt one of the answers to this question? Commented Dec 9, 2019 at 12:20
  • Do you just want the lines to be between those particular times on those particular dates? Commented Dec 9, 2019 at 14:11

3 Answers 3

1

How about Miller ?

$ mlr --nidx --repifs filter -S '
    t = strptime($3,"%H:%M:%S"); 
    t > strptime("23:00","%H:%M") || t < strptime("01:00","%H:%M")
' input
Dec 8 23:00:05 hermes kernel: [<ffffffff8118a6f0>] warn_alloc_failed+0x110/0x180
Dec 8 23:34:45 hermes kernel: [<ffffffff816a204a>] __alloc_pages_slowpath+0x6b6/0x724
Dec 8 23:54:45 hermes kernel: [<ffffffff81424400>] ? misc_open+0x40/0x1c0
Dec 9 00:34:45 hermes kernel: [<ffffffff8118ec85>] __alloc_pages_nodemask+0x405/0x420
Dec 9 00:54:45 hermes kernel: [<ffffffff8118ec85>] __alloc_pages_nodemask+0x405/0x420
0

If I understand you correctly, with GNU grep:

grep -e "00:[0-5][0-9]:[0-5][0-9]" -e "23:[0-5][0-9]:[0-5][0-9]" in_file > out_file

Look for de pattern 00:[<0 to five><0 to nine>]:[<0 to five><0 to nine>]

or the pattern 23:[<0 to five><0 to nine>]:[<0 to five><0 to nine>]

2
  • Thanks for your answer. But I forgot something related to the my issue . I have updated my issue. Could you please help to me? Can you clarify me your grep command that you have write ? Commented Dec 9, 2019 at 13:12
  • Sorry, I just grep 23 and forgot 00:00, I'll come back. Commented Dec 9, 2019 at 13:36
0

With perl, assuming you want to start 11pm Dec 8 and extend for 2 hours:

perl -MTime::Piece -lane '
    BEGIN {
        $start = Time::Piece->strptime("Dec 8 23:00:00", "%b %d %T");
        $stop = $start + 2*3600;
    }
    $t = Time::Piece->strptime("@F[0,1,2]", "%b %d %T");
    print if $start <= $t && $t <= $stop;
' file

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.