sed 中的正则表达式用于搜索和替换(无效参考) [英] regex in sed for search & replace (invalid reference)
本文介绍了sed 中的正则表达式用于搜索和替换(无效参考)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用 shell 和正则表达式替换一行中的字符串.
I would like to replace a string in a line with shell and regex.
例如,在文件 configuration.php
中,我想将 TO_REPLACE
替换为 OK_REPLACE
:
For example, in file configuration.php
I would like to replace TO_REPLACE
with OK_REPLACED
:
public $user = 'TO_REPLACE';
我试过这个命令:
cd ~/public_html; sed -i "s/^\public $user = *'[^']*'/\1OK_REPLACED'/g" configuration.php
但我收到此错误
sed: -e expression #1, char 39: invalid reference \1 on `s' command's RHS
我也试过这个,但没有
sed -i "s/^\(public \$user = *')[^']*'/\1OK_REPLACED'/g" configuration.php
推荐答案
我认为你的括号必须是平衡的.您的第一个带有反斜杠前缀,但第二个不是.试试这个:
I think your parenthesis must be balanced. Your first one is prefixed with a backslash, but the 2nd one is not. Try this:
sed -i "s/^\(public \$user = *'\)[^']*'/\1OK_REPLACED'/g" configuration.php
或者这个:
sed -r -i "s/^(public \$user = *')[^']*'/\1OK_REPLACED'/g" configuration.php
这篇关于sed 中的正则表达式用于搜索和替换(无效参考)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文