如何使用grep或类似工具排除匹配周围的几行? [英] How to exclude several lines around match with grep or similar tool?
问题描述
我可以使用 grep -v 'my_pattern' some_file
找到所有不匹配的文件行.此外,我可以使用 grep 的 -A
、-B
或 -C
选项在匹配之前/之后/左右打印几个字符串.但我不能结合这两个选项来排除具有模式的行和匹配行附近的一定数量的行 - grep 结果显示整个文件.例如,我有很多这样的模式的日志:
I can find all non-matched lines of file with grep -v 'my_pattern' some_file
. Also i can print few strings before/after/around match with -A
, -B
or -C
options of grep. But i can't combine these two options to exclude lines with pattern and certain amount of lines near matched lines - grep shows entire file as result. For example, i have log with a lot of patterns like this:
25.02.2012 10:41:37 here goes memory state
25MiB free
16MiB allocated
max free block is 4MiB
我想过滤它们.当然,我可以编写自定义的 perl/awk 脚本,但是有没有更优雅的方法来做到这一点?
I'd like to filter them. Of course, i can write custom perl/awk script, but is there more elegant way to do this?
推荐答案
您可以使用 vim
文本编辑器:
You can use the vim
text editor:
:g/my_pattern/-2,//+2d
这篇关于如何使用grep或类似工具排除匹配周围的几行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!