我在sed命令中使用replace时遇到一个问题。我有一个名为的文件a.txt
,我想用其他一行替换一行,但出现上述错误。
我要替换的代码:
DOMAIN_LOCAL = "http://127.0.0.1:3000";
我希望将其替换为任何其他IP,让它说如下:-
DOMAIN_LOCAL = "http://127.1.1.2:3000";
我尝试过的:
ip=http://127.1.1.2:3000
sed "s/DOMAIN_LOCAL = .*$/DOMAIN_LOCAL = "$ip";/g" a.txt
但这给了我以下错误。有人可以帮忙吗?
sed: 1: "s/DOMAIN_LOCAL = .*$/DO ...": bad flag in substitute command: '/'
您将需要使用另一个定界符。并转义$
或使用单引号:
% ip="http://127.1.1.2:3000"
% sed 's~DOMAIN_LOCAL = .*$~DOMAIN_LOCAL = "'"$ip"'";~' a.txt
DOMAIN_LOCAL = http://127.1.1.2:3000;
当您/
在s
ubstitute命令中用作分隔符时,它将在以下位置的第一个斜杠处终止http://
:
sed 's/DOMAIN_LOCAL = .*$/DOMAIN_LOCAL = http://127.1.1.2:3000;/'
# ^ here
分解:
sed 's~DOMAIN_LOCAL = .*$~DOMAIN_LOCAL = "'"$ip"'";~' a.txt
# │ ││└ Use double quotes to avoid word splitting and
# │ ││ globbing on $ip
# │ │└ Exit single quotes
# │ └ Literal double quotes
# └ Using single quotes to avoid having to escape special characters
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句