开发者

Use sed to delete a matched regexp and the line (or two) underneath it

开发者 https://www.devze.com 2022-12-16 21:01 出处:网络
OK I found this question: How do I delete a matching line, the line above and the one below it, using sed?

OK I found this question:

How do I delete a matching line, the line above and the one below it, using sed?

and just spent the last hour trying to write something that will match a string and delete the line containing the string and the line beneath it (or a variant开发者_JAVA技巧 - delete 2 lines beneath it).

I feel I'm now typing random strings. Please somebody help me.


If I've understood that correctly, to delete match line and one line after

/matchstr/{N;d;}

Match line and two lines after

/matchstr/{N;N;d;}
  • N brings in the next line
  • d - deletes the resulting single line


you can use awk. eg search for the word "two" and skip 2 lines after it

$ cat file
one
two
three
four
five
six
seven
eight
$ awk -vnum=2 '/two/{for(i=0;i<=num;i++)getline}1' file
one
five
six
seven
eight
0

精彩评论

暂无评论...
验证码 换一张
取 消