[英]Replace content of matched regexp-address without repeating regex
Is there a way in (GNU) sed to perform a s
substitution on the exact string matched by the regexp-address? (GNU) sed 中是否有办法对正则表达式地址匹配的确切字符串执行
s
替换?
Something that avoids repeating the regexp already used to select the line?避免重复已经用于选择行的正则表达式的东西?
Example of an hard-to-read sed
invocation where the same regexp is used to match the line where the s
command will operate as well as to determine what will be substituted in that line.一个难以阅读的
sed
调用示例,其中使用相同的正则表达式来匹配s
命令将运行的行以及确定将在该行中替换的内容。
sed -i -e '/verycomplexregexp/s/verycomplexregexp/something/'
Using an empty regexp //
in the s
command avoids having to repeat the regexp used to select the line:在
s
命令中使用空的正则表达式//
避免了重复用于选择行的正则表达式:
sed -i -e '/verycomplexregexp/s//something/'
The empty regexp //
refers the previously matched text in the pattern space.空的正则表达式
//
指的是模式空间中先前匹配的文本。 When a regexp-address is used, the empty regexp points to the string that made the line match the address.当使用 regexp-address 时,空的 regexp 指向使该行与地址匹配的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.