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:
data from 2012 Jun to 2012 Jul
data from 2012 Jun to 2012 Jul 07
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
pamu
August 23, 2012, 9:16am
2
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
methyl
August 23, 2012, 9:26am
3
@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 ?
pamu
August 23, 2012, 9:32am
4
Thanks methyl,
Yes...
That's why i have given two outputs so it can be cleared both advantages and disadvantages of this..
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
methyl
August 23, 2012, 9:51am
6
@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:)
Lem
August 24, 2012, 6:56am
8
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