0

This is my LogFile(Test.Log):

May 10 01:06:05 A
May 10 01:06:06 B
May 10 01:06:07 C
May 10 01:07:01 D
May 10 01:07:02 E
May 10 01:07:09 F
May 10 01:07:11 G
May 10 01:07:11 H
May 10 01:07:12 I
May 10 01:07:13 J
May 10 01:07:15 k

I want to print the data between the specified Starttime and Endtime.

I have entered the code in a Sample.sh File, which is,

BEGIN { StartFound = 0;StartNotFound = 0; EndFound=0; EndNotFound=0; }
{
if($0 ~ Start)
{
     StartFound = 1;
     StartNotFound = 0; 
     EndNotFound=0;
     EndFound=0;
}
else   
{
    StartNotFound = 1;
    StartFound = 0;
    EndNotFound=0;
    EndFound=0;
}

if($0 ~ End)
{
    EndFound=1;
    StartNotFound = 0; 
    StartFound = 0;
    EndNotFound=0;

}
else
{
    StartFound = 0;
    StartNotFound = 0;
    EndNotFound = 1;
    EndFound=0;
 }

if(StartFound)
{
   ANS = (Start <= $3 && $3 <= End);          
   print ANS;
}
if(StartNotFound)
{      
   ANS = (Start < $3 && $3 <= End);          
   print ANS; 
}
if(EndNotFound)
{
     ANS = (Start <= $3 && $3 < End)
     print ANS;   
}
if(EndFound)
{
   ANS = (Start <= $3 && $3 <= End);          
   print ANS;
}
}

In the GitBash I have used the following,

$ awk -v start=01:07:08 -v stop=01:07:14 -f Sample.sh Test.log

For which it should display the following,

    May 10 01:07:09 F
    May 10 01:07:11 G
    May 10 01:07:11 H
    May 10 01:07:12 I
    May 10 01:07:13 J.

And also if my input is

 $ awk -v start=01:07:08 -v stop=01:07:11 -f Sample.sh Test.log

Then my output should be,

May 10 01:07:09 F
May 10 01:07:11 G
May 10 01:07:11 H

But I am not getting the desired output. How can I improve this code?

2

1 Answer 1

0

Try this:

awk -v start="01:07:08" -v end="01:07:11" '$3 >= start { keep=1 } $3 > end { exit } keep { print }' Test.log

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .