grep的-v模式,同时删除之前,1号线和4号线后, [英] grep -v pattern and also remove 1 line before and 4 lines after

查看:99
本文介绍了grep的-v模式,同时删除之前,1号线和4号线后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想到grep的模式,并删除匹配模式的行之前也是1号线和上下文后4行。我想:

I would like to grep a pattern and remove the line of the matching pattern and also 1 line before and 4 lines after the context. I tried:

grep -v -A 4 -B 1

在此先感谢!

例如:

Rule: r1
Owner: Process explorer.exe Pid 1544
0x01ec350f  8b 45 a8 0f b6 00 8d 4d a8 ff 14 85 c8 7f ed 01   .E.....M........
0x01ec351f  84 c0 75 ec 8b 4d fc e8 ba f5 fe ff f7 85 b0 fd   ..u..M..........
0x01ec352f  ff ff 00 00 01 00 75 13 33 c0 50 50 50 68 48 28   ......u.3.PPPhH(
0x01ec353f  eb 01 33 d2 8b cb e8 b0 57 ff ff f7 05 8c 9b ed   ..3.....W.......

我想给grep的explorer.exe,并删除之前的行以及1号线和4后行。

I would like to grep "explorer.exe" and remove the line and also 1 line before and 4 lines after.

推荐答案

这AWK一行代码将有助于:

awk

this awk one-liner would help:

 awk  'NR==FNR{if(/explorer[.]exe/)d[++i]=NR;next}
      {for(x=1;x<=i;x++)if(FNR>=d[x]-1&&FNR<=d[x]+4)next}7' file file

看到这样的例子:

see this example:

kent$  cat f
foo
foo2
Rule: r1
Owner: Process explorer.exe Pid 1544
remove1
remove2
remove3
remove4
bar
bar2

kent$  awk  'NR==FNR{if(/explorer[.]exe/)d[++i]=NR;next}{for(x=1;x<=i;x++)if(FNR>=d[x]-1&&FNR<=d[x]+4)next}7' f f 
foo
foo2
bar
bar2

的vim

如果VIM也有可能对你来说,它可能是一个容易得多:

vim

if vim is also possible for you, it could be a lot easier:

:g/Pattern/norm! k6dd

请注意,vim的解决办法有问题,在首场比赛中,如果你的模式是在你的文件中的第一行。

Note, the vim solution would have problem in first match if your pattern was on the 1st line in your file.

这篇关于grep的-v模式,同时删除之前,1号线和4号线后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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