sed - comment a matching line and x lines after it

怎甘沉沦 提交于 2019-12-02 20:54:33

You can do this by applying a regular expression to a set of lines:

sed -e '/myprocess/,+4 s/^/#/' 

This matches lines with 'myprocess' and the 4 lines after them. For those 4 lines it then inserts a '#' at the beginning of the line.

(I think this might be a GNU extension - it's not in any of the "sed one liner" cheatsheets I know)

sed '/\[myprocess/ { N;N;N;N; s/^/#/gm }' input_file

Using string concatenation and default action in awk.
http://www.gnu.org/software/gawk/manual/html_node/Concatenation.html

awk '/myprocess/{f=1} f>5{f=0} f{f++; $0="#" $0} 1'  foo.txt

or if the block always ends with empty line

awk '/myprocess/{f=1} !NF{f=0} f{$0="#" $0} 1'  foo.txt
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!