Bash 在一行中查找10的模式,并在某个搜索字符串后重置

Bash 在一行中查找10的模式,并在某个搜索字符串后重置,bash,awk,Bash,Awk,我有一个日志文件(见下面的示例)。我想返回启动周期中连续10次出现“监视器显示图像”的第一行(在“框打开”之后,在“框关闭”之前)。我猜是AWK,但不确定如何实现这一点。任何帮助都将不胜感激 日志示例: Box turned on. Monitor is NOT displaying image. Monitor is NOT displaying image. Monitor is NOT displaying image. Monitor is NOT displaying imag

我有一个日志文件(见下面的示例)。我想返回启动周期中连续10次出现“监视器显示图像”的第一行(在“框打开”之后,在“框关闭”之前)。我猜是AWK,但不确定如何实现这一点。任何帮助都将不胜感激

日志示例:

Box turned on.
 Monitor is NOT displaying image.
 Monitor is NOT displaying image.
 Monitor is NOT displaying image.
 Monitor is NOT displaying image.
 Monitor is NOT displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Monitor is displaying image.
 Box off.
 Pausing before reboot (10).
 Pausing before reboot (9).
 Pausing before reboot (8).
 Pausing before reboot (7).
 Pausing before reboot (6).
 Pausing before reboot (5).
 Pausing before reboot (4).
 Pausing before reboot (3).
 Pausing before reboot (2).
 Pausing before reboot (1).
 Box turned on.

awk
救援

$ awk '/Box turned on/{f=1}
             /Box off/{f=0} 
                     f{if(/Monitor is displaying image/)c++; else c=0}
                 c==10{print; exit}' log

当你说“返回”时,你实际上是指“打印”还是其他什么(如果是,是什么)?为什么你会在意打印的是第一行、最后一行还是其他行?你是在输出中寻找行号还是什么?如果要查找的模式在输入文件中多次出现—输出多行或在第一行或其他行之后退出,该怎么办?编辑你的问题,使其包含给定输入的预期输出,以及迄今为止你尝试过的任何内容。是的,我喜欢AWK就是因为这个原因。感谢您抽出时间回复。