sed-注释匹配的行和其后的x行 [英] sed - comment a matching line and x lines after it
问题描述
我需要使用sed注释匹配行和其后的4行的帮助. 在文本文件中.
I need help with using sed to comment a matching lines and 4 lines which follows it. in a text file.
我的文本文件是这样的:
my text file is like this:
[myprocess-a]
property1=1
property2=2
property3=3
property4=4
[anotherprocess-b]
property1=gffgg
property3=gjdl
property2=red
property4=djfjf
[myprocess-b]
property1=1
property4=4
property2=2
property3=3
我想在所有带有文本"[myprocess"的行以及所有紧跟其后的4行的行前添加# 预期输出:
I want to prefix # to all the lines having text '[myprocess' and 4 lines that follows it expected output:
#[myprocess-a]
#property1=1
#property2=2
#property3=3
#property4=4
[anotherprocess-b]
property1=gffgg
property3=gjdl
property2=red
property4=djfjf
#[myprocess-b]
#property1=1
#property4=4
#property2=2
#property3=3
非常感谢您的帮助.
推荐答案
您可以通过对一组行应用正则表达式来做到这一点:
You can do this by applying a regular expression to a set of lines:
sed -e '/myprocess/,+4 s/^/#/'
这将匹配'myprocess'的行和它们后面的4行.对于这4行,然后在该行的开头插入一个#".
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.
(我认为这可能是GNU扩展-它不在我所知道的任何"sed单班轮"备忘单中)
(I think this might be a GNU extension - it's not in any of the "sed one liner" cheatsheets I know)
这篇关于sed-注释匹配的行和其后的x行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!