这是我目前的代码.我试图用另一个文件名替换文件中的文件名字符串.但我目前正在收到错误
"sed:1:"s/directory ="[A-Za-z0 ...":替换命令中的坏标志:'U'"
这段代码有什么问题?
function restart_existing ()
{
old="directory = \"[A-Za-z0-9\/]\""
new="directory = \"$1\""
sed -i '' "s/$old/$new/" "$HOME/angelpretendconfig"
}
restart_existing "$HOME/blahblahblah/shoot/blah"
Run Code Online (Sandbox Code Playgroud)
编辑:谢谢!我已经采纳了你的建议,并修改了代码.
function restart_existing ()
{
old="directory = \"*\""
printf -v new 'directory = "%s"' "$1"
sed -i '' "s;$old;$new;" "$HOME/angelpretendconfig"
}
restart_existing "Query"
Run Code Online (Sandbox Code Playgroud)
但现在有问题的话来自
directory = "/home/jamie/bump/server"
directory = "Query"/home/jamie/bump/server"
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
当您要替换的内容包含正斜杠时,请勿在sed中使用正斜杠:
$ sed 's;foo/bar;baz/wuz;' <<< "where is the foo/bar?"
where is the baz/wuz?
Run Code Online (Sandbox Code Playgroud)
此外,有时避免手动转义引号更具可读性:
function restart_existing ()
{
old='directory = "[A-Za-z0-9\/]"'
printf -v new 'directory = "%s"' "$1"
sed -i '' "s;$old;$new;" "$HOME/angelpretendconfig"
}
restart_existing "$HOME/blahblahblah/shoot/blah"
Run Code Online (Sandbox Code Playgroud)