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

谁说胖子不能爱 提交于 2020-01-05 05:29:09

问题


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 - delete 2 lines beneath it).

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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/2061131/use-sed-to-delete-a-matched-regexp-and-the-line-or-two-underneath-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!