如何排除匹配周围的几行使用grep或类似工具? [英] How to exclude several lines around match with grep or similar tool?

查看:119
本文介绍了如何排除匹配周围的几行使用grep或类似工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能找到的文件以的grep -v'my_patternSOME_FILE 所有的非匹配行。此外,我可以打印弦数前/后/左右比赛与 -A -B 的grep -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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆