Grep -v, and also remove 1 line before and 4 lines after

I would like to grep the template and delete the line of the corresponding template, as well as 1 line before and 4 lines after the context. I tried:

grep -v -A 4 -B 1

Thanks in advance!

Example:

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.......

I would like to grep "explorer.exe" and delete the line, as well as 1 line before and 4 lines after.

+4
source share
1 answer

Awk

this one line awk will 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

if vim is also possible for you, this could be much simpler:

:g/Pattern/norm! k6dd

Please note: the vim solution will have problems in the first match if yours patternwas on the first line of your file.

+5
source

Source: https://habr.com/ru/post/1568598/


All Articles