Logging 如何在连续的行中找到相同的字符串,然后打印连续包含它们的所有行

Logging 如何在连续的行中找到相同的字符串,然后打印连续包含它们的所有行,logging,awk,Logging,Awk,我有一个ping日志文件,格式为 2021/02/15 14:22:27 : Reply[1] from 10.10.10.1: bytes=32 time=31.9 ms TTL=244 jitter=0.00 ms 2021/02/15 14:22:27 : Reply[2] from 10.10.10.1: bytes=32 time=32.5 ms TTL=244 jitter=0.03 ms 2021/02/15 14:22:28 : 10.10.10.1: request timed

我有一个ping日志文件,格式为

2021/02/15 14:22:27 : Reply[1] from 10.10.10.1: bytes=32 time=31.9 ms TTL=244 jitter=0.00 ms
2021/02/15 14:22:27 : Reply[2] from 10.10.10.1: bytes=32 time=32.5 ms TTL=244 jitter=0.03 ms
2021/02/15 14:22:28 : 10.10.10.1: request timed out
2021/02/15 14:22:28 : Reply[4] from 10.10.10.1: bytes=32 time=29.9 ms TTL=244 jitter=0.28 ms
2021/02/15 14:22:29 : Reply[5] from 10.10.10.1: bytes=32 time=27.4 ms TTL=244 jitter=0.42 ms
2021/02/15 14:22:29 : Reply[6] from 10.10.10.1: bytes=32 time=31.3 ms TTL=244 jitter=0.63 ms
2021/02/15 14:22:30 : 10.10.10.1: request timed out
2021/02/15 14:22:31 : 10.10.10.1: request timed out
2021/02/15 14:22:31 : 10.10.10.1: request timed out
2021/02/15 14:22:32 : Reply[10] from 10.10.10.1: bytes=32 time=33.8 ms TTL=244 jitter=0.91 ms
我只是在寻找有2次或更多次没有回复的行(一次只有一次超时是可以的)

我试过使用awk,但问题是它打印出的每一行都符合我的要求,除了最后一行

awk -F " : " "($2 !~ /Reply/ && $2 == prev2) {print prevline} {prev2 = $2; prevline = $0} <file>

由于它发生在两行或更多的连续行上,但awk i只打印出前两行,而不是最后一行(它不会打印14:22:28的超时,因为它只发生一次,但这是预期的!)

对于您显示的示例,请尝试以下内容

awk '
!/request timed out/{
  if(count>=2){ print val }
  val=""
  count=0
}
/request timed out/{
  count++
  val=(val?val ORS:"")$0
}
END{
  if(count>=2){ print val }
}
'  Input_file
说明:添加上述内容的详细说明

awk '                           ##Starting awk program from here.
!/request timed out/{           ##Checking condition if line is NOT having request timed out then do following.
  if(count>=2){ print val }     ##Checking condition if count is greater than equal to 2 then print val here.
  val=""                        ##Nullify val here.
  count=0                       ##Setting count to 0 here.
}
/request timed out/{            ##Checking condition if request timed out found in line then do following.
  count++                       ##Increasing count value with 1 here.
  val=(val?val ORS:"")$0        ##Adding line into val and keep concatenating its value to it.
}
END{                            ##Starting END block of this code here.
  if(count>=2){ print val }     ##Checking condition if count is greater than equal to 2 then print val here.
}
'  Input_file                   ##mentioning Input_file name here.

像这样的东西应该行得通,也许可以进一步简化

$ awk '/Reply/{c=0} c==1{print p} c; !/Reply/{c++; p=$0}' file

2021/02/15 14:22:30 : 10.10.10.1: request timed out
2021/02/15 14:22:31 : 10.10.10.1: request timed out
2021/02/15 14:22:31 : 10.10.10.1: request timed out
保留一个计数器
c
,对感兴趣的连续行进行计数。保留第一份的副本,仅当我们仍在连续块中时才打印(意思是
c
未重置)。继续在块中打印。最后一个语句有一些冗余,因为它总是保留前一行,但没有太大影响


c
c=0{print}

欢迎来到SO,感谢您以代码的形式展示您的努力。请从您的问题中删除
,并添加更多清晰的输入和预期输出示例,以便更好地理解您的问题,谢谢。另外,请发布一个可测试的示例输入文件,说明您试图解决的问题。只有一个“回复”,所以不清楚你想过滤掉什么。谢谢你,按照要求编辑帖子这是一个更简单的解决方案,它可以工作,但是你能解释一下当你在第一行末尾“调用”c时会发生什么吗?这个->..}c;!/。。。在这个基础上构建RavinderSingh13的答案将是awk'/Reply/{c=0}c=={print p}c>{print}/Reply/{c++;p=(p?p OR:)$0}是否使它可以与任何连续的行一起工作?例如,每4行或更多行?尝试此解决方案,不幸的是,我在windows上使用awk(我知道,公司笔记本电脑不能有虚拟机),但它无法运行,错误:val=“unterminated string(即使我有val=”“双引号)@blue212121,这是一个在Linux box中成功测试的代码,我没有windows环境,因此无法在那里测试:)您有哪个版本的
awk
?上面写着GNU awk 3.1。6@blue212121,理想情况下,它应该与该版本一起工作,但不确定windows版本中是否有其他内容。ah必须在cmdprompt…w中使用\来转义它我现在很有魅力谢谢你!
$ awk '/Reply/{c=0} c==1{print p} c; !/Reply/{c++; p=$0}' file

2021/02/15 14:22:30 : 10.10.10.1: request timed out
2021/02/15 14:22:31 : 10.10.10.1: request timed out
2021/02/15 14:22:31 : 10.10.10.1: request timed out