如何使用sed用新路径替换文件中的路径?

use*_*227 0 sed filenames

我正在使用 sed 用新路径替换文件中的路径。这里缺少什么?

# sed -i `s|/$SPLUNK_HOME/bin/splunk|/opt/splunk/bin/splunk|g' filename
Run Code Online (Sandbox Code Playgroud)

我接受>它。

Dop*_*oti 5

你用反引号 ( `) 打开一个你永远不会终止的子 shell 语句。确保您的报价匹配。

>提示是shell告诉你,它是由工作时可在规定的二次提示寻找一个未终止的报价更多的投入PS2

您想在脚本中使用参数扩展,因此弱引号 ( ") 是要使用的引号。

例如:

$ cat haystack
I found some straw in here!
$ needle=straw
$ sed "s/$needle/pins/" haystack
I found some pins in here!
Run Code Online (Sandbox Code Playgroud)

让我们来看看使用弱引号 ( ") 和强引号 ( ')时发生的情况之间的区别:

$ set -x
$ sed "s/$needle/pins/" haystack
+ sed s/straw/pins/ haystack
I found some pins in here!
$ sed 's/$needle/pins/' haystack
+ sed 's/$needle/pins/' haystack
I found some straw in here!
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,使用弱引号,for 的参数扩展$needle是在 shell 的要求下发生的,然后才 sed看到它。使用强引号,这不会发生,因此sed现在搜索正则表达式/$needle/,即“输入结束后跟字符串needle”,它永远不会匹配任何内容,因此不进行替换。