[英]SED change text between matches without affecting the matches
I have some text 我有一些文字
#MATCH1
command 1
command 2
command 3
#MATCH2
sed -i '/MATCH1/,/MATCH2/' s/^/#/' <filename>
Does 是否
##MATCH1
#command 1
#command 2
#command 3
##MATCH2
And what i want is 我想要的是
#MATCH1
#command 1
#command 2
#command 3
#MATCH2
Anyone has any idea how to do this? 有人知道如何执行此操作吗? Without post processing the output
无需后处理输出
Thank you 谢谢
You may use this sed
that make sure we don't have #
at line start: 您可以使用此
sed
来确保在行首没有#
号:
sed '/MATCH1/,/MATCH2/ s/^[^#]/#&/' file
#MATCH1
#command 1
#command 2
#command 3
#MATCH2
Note that this only matches non-empty lines between given keywords. 请注意,这仅匹配给定关键字之间的非空行。 Alternatively you may use this
sed
: 或者,您可以使用此
sed
:
sed '/MATCH1/,/MATCH2/ { /^#/! s/^/#/; }' file
sed is for doing s/old/new, that is all. sed是用于s / old / new,仅此而已。 For anything else you should be using awk:
对于其他任何事情,您都应该使用awk:
$ awk '/MATCH2/{f=0} f{$0="#" $0} /MATCH1/{f=1} 1' file
#MATCH1
#command 1
#command 2
#command 3
#MATCH2
That will work using any awk in any shell on any UNIX box and is utterly trivial to modify if/when you want to do anything else. 在任何UNIX盒子上的任何shell中使用任何awk都可以使用,并且在/当您要执行其他任何操作时完全可以轻松修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.