我需要提取/打印行直到停止模式。这需要在开始模式每第三次出现之后发生,即ERROR_FILE_NOT_FOUND
持续到文件末尾。以下是日志文件的示例。
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
Extract line 1
Extract line 2
Extract line 3
Stop_pattern
some text
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
Extract line 5
Extract line 6
Extract line 7
Stop_pattern
some text
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
Extract line 9
Extract line 10
Extract line 11
Stop_pattern
期望的输出:
Extract line 1
Extract line 2
Extract line 3
//separate if possible
Extract line 5
Extract line 6
Extract line 7
//separate if possible
Extract line 9
Extract line 10
Extract line 11
//separate if possible
答案1
在 awk 中:
$ awk '/Stop_pattern/{c=0; print ""}; c>=3;/ERROR_FILE_NOT_FOUND/{c++} ' file
Extract line 1
Extract line 2
Extract line 3
Extract line 5
Extract line 6
Extract line 7
Extract line 9
Extract line 10
Extract line 11