myfile.log 包含以下内容:
----- 12:08:12 (123.123.123.123) --------------------------------
Some code here...
This section has no specified length...
-----------------------------------------------------------------
----- 12:10:15 (254.14.187.123) --------------------------------
Some code here...
This section has no specified length...
It could be 3 lines, or 4 lines, or 20 lines.
-----------------------------------------------------------------
我想使用 grep 之类的东西来提取每个包含 IP 地址 123.123.123.123 的 block 。例如,将返回以下内容:
----- 12:08:12 (123.123.123.123) --------------------------------
Some code here...
This section has no specified length...
-----------------------------------------------------------------
编辑:
感谢到目前为止的答案,我忘了提到我需要它以相同的方式流式传输输出
tail
将。
最佳答案
我会使用类似的东西:
sed -n '/^-----.*(123\.123\.123\.123) ---/,/^------/p' file.log
它首先匹配一行,以 5 个连字符开头,然后包含多个任意字符,然后是括号中的 IP 地址,然后是一些连字符。然后它匹配所有行,直到遇到以 5 个连字符开头的行。您可以使用
/^-\{5,\}$/
指定仅包含连字符的行(至少 5 个)。关于您的问题的编辑:我假设您的意思是流式传输
tail -f
?只需将其输出通过管道传输到 sed:tail -f file.log | sed -n '/^-----.*(123\.123\.123\.123) ---/,/^------/p'
关于unix - ubuntu 中的模式匹配日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9064006/