我需要提取/打印行的帮助,直到停止模式。这需要在每第3次启动模式发生后发生,并持续到文件结束。下面考虑一下日志文件的示例。 我的开始模式将是ERROR_FILE_NOT_FOUND每隔3次出现,停止模式将是Stop_pattern
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
some text
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
some text
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
some text
Extract line 9
Extract line 10
Extract line 11
Stop_pattern
期望的输出:
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
答案 0 :(得分:0)
这可能适合你(GNU sed):
sed -n '/ERROR_FILE_NOT_FOUND/{:a;$!{N;s//&/3;Ta;n;N;N;p}}' file
收集行,直到找到3个字符串,然后打印接下来的3行。