Get the lines from logfile within start and end date

Hi guys,

I am having the below logfile,date in yyyy-mm-dd

 
2013-08-02 *some content*
2013-08-02 *some content*
2013-08-02 *some content*
2013-08-03 *some content*
2013-08-05 *some content*

from the above logfile i need to get the lines between the two timestamps,if i give

2013-08-02 (start) &  2013-08-05 (end)

i am getting the content.
But wen i give

2013-08-01  (start) and 2013-08-05 (end)

i didnt get the output.
my reqiurement is even though 2013-08-01 is not there wen i give 2013-08-01 as start date it should calculate and give the lines.
Please help me guys.
I think this can be done through some date manipulation.

st="2013-08-01"
en="2013-08-03"

awk -v ST="$st" -v EN="$en" '
        {
                V = $1
                gsub ( "-", X, V )
                gsub ( "-", X, ST )
                gsub ( "-", X, EN )
                if ( V >= ST && V <= EN )
                        print
        }
' file.log

recommend to move below lines to BEGIN{} part in awk, which will avoid to gsub on each line again.

                gsub ( "-", X, ST )
                gsub ( "-", X, EN )
2 Likes

Hi guys thanks for your reply,
when i run the above script i didnt get any output.

 
[ 2013-08-02 04:01:23 pid=15642 ppid=15642 ]  Exited the Programme
[ 2013-08-02 03:59:14 pid=15690  ] Exited the Programme, recovery completed
[ 2013-08-03 09:15:37 pid=21374   ] Exited the Programme, recovery completed 
[ 2013-08-03 09:19:49 pid=21773   ] Exited the Programme, recovery 
[ 2013-08-04 09:26:44 pid=22199  ] Exited the Programme, recovery completed 

the code i run is

#!/bin/ksh
st="2013-08-01"
en="2013-08-03"
awk -v ST="$st" -v EN="$en" '
{
V = $1
gsub ( "-", X, V )
gsub ( "-", X, ST )
gsub ( "-", X, EN )
if ( V >= ST && V <= EN )
print
}
' recovery.log

Make V to $2 on your script since your actual file has a [ in the beginning.

V = $2
1 Like

hi thank u its working fine:o