Fetch data between two dates from a file

Hi All,

I m new to this forum & UNix too.
currently i have a requirement which can fetch data from a logfile between two dates or timestamp.

for example:

  1. data from 2012 Jun to 2012 Jul
  2. data from 2012 Jun to 2012 Jul 07
  3. data from 2012 Jun 16 10:20 to 2012 Jul 03 10:10

Please note that the fetched data shoul contain logs of both the dates including repeating logs.

Could you please help me solve it with awk command?

Currently i am using

awk -v s="$start" -v e="$end" '$0~s,$0~e' <logfile

Its working fine if there is only single records for the destination date.
But if we have many records like

2012 Jun sdfsdjfljsdjfs
2012 Jul gjdflgldfgkdfgk
2012 Jul XXXXXXX
2012 Jul ZZZZZZZ

Then for data between 2012 Jun to 2012 Jul

It is displaying only

2012 Jun sdfsdjfljsdjfs
2012 Jul gjdflgldfgkdfgk

whereas i need like:

2012 Jun sdfsdjfljsdjfs
2012 Jul gjdflgldfgkdfgk
2012 Jul XXXXXXX
2012 Jul ZZZZZZZ

So please help me to get the expected output.

Thanks in advance!
KD

If you know exact date you can use this...

$ cat file1
2012 Jun sdfsdjfljsdjfs
2012 Jul gjdflgldfgkdfgk
2012 Jul XXXXXXX
2012 Jul ZZZZZZZ
$ sed -n '/2012 Jun/,/2012 Jul ZZZZZZZ/p' file1
2012 Jun sdfsdjfljsdjfs
2012 Jul gjdflgldfgkdfgk
2012 Jul XXXXXXX
$ sed -n '/2012 Jun/,/2012 Jul/p'  file1
2012 Jun sdfsdjfljsdjfs
2012 Jul gjdflgldfgkdfgk

@pamu
The sed code contains the same bug as the original awk code ! Both of them stop after the first occurance of the end string.

Correction. The output from the first sed is not posted correctly (there should be one more output record). If the stop string is actually the whole contents of the last record for the month, the sed works. This does not however match the specification.

I can't think of a string processing method to process alphabetic date ranges. They are going to have to be converted to a numeric form and compared as YYYYMMDDHHMMSS or perhaps seconds since the Epoch.
Do you have:
Perl ?
GNU date ?

Thanks methyl,:slight_smile:

Yes...
That's why i have given two outputs so it can be cleared both advantages and disadvantages of this..:slight_smile:

Assuming that the ending patterns are adjacent (if repeated), this should work:

awk '/2012 Jun/{p=1}
!/2012 Jul/ && prev~/2012 Jul/ && p{p=0}
{prev=$0}p' file
1 Like

@elixir_sinari
Brilliant piece of code. It should work for a three scenarios in post #1.

sorry for late thanks.

But really thanks a lot every one who suggested their ideas.
The Elixir codes really worked in all the scenarios.
once again thank You:):slight_smile:

With sed I think we'd need two runs:

lastline=$(sed -n '/^2012 Jul/ =' file |tail -n1)
sed -n "/2012 Jun/,${lastline:-$} p" file

--
Bye

1 Like