如何获取日志文件中指定开始时间和结束时间之间的数据?

如何获取日志文件中指定开始时间和结束时间之间的数据?

这是我的日志文件(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

我想打印指定的开始时间和结束时间之间的数据。

我已在 Sample.sh 文件中输入代码,即

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;
}
}

在 GitBash 中我使用了以下内容,

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

它应该显示以下内容,

    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.

而且如果我的输入是

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

那么我的输出应该是,

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

但我没有得到所需的输出。我该如何改进这段代码?

答案1

尝试这个:

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

相关内容