Vam*_*esh 0 sed text-processing
在我的 grunt 文件中,我有这样的命令
"sed -i index 's/API_CONTEXT_URI/http:\\/\\/localhost:5557/g' www/index.js"
Run Code Online (Sandbox Code Playgroud)
这不是我写的文件;这必须作为一个维护项目来找我。所以我不能正确理解那条线。它应该用 中提供的 URL 替换字符串 API_CONTEXT_URI index.js。错误是
can't read s/API_CONTEXT_URI/http://localhost:5557/g: No such file or directory
Run Code Online (Sandbox Code Playgroud)
根据我的解释,sed找不到index.js. 但有一个index.js中www的文件夹。我曾尝试更改\\/为\escape /,但仍然无法正常工作。大家能帮我看看哪里吗?我对命令中的-i和 表示怀疑index。
-es/foo/bar/ (*) 之前缺少一个有一个困惑,你是(脚本)编辑index还是www/index.js?
如果index是用于生成的模板文件(带有 API_CONTEXT_URL)www/index.js,我建议
sed -e s,API_CONTEXT_URL,http://localhost:5557,g index > www/index.js
Run Code Online (Sandbox Code Playgroud)
请注意,您可以使用任何聊天作为替代品之间的分隔符,我使用逗号 (,) 来避免过多的转义。
如果要编辑的文件是www/index.js,请使用
sed -i -e s,API_CONTEXT_URL,http://localhost:5557,g www/index.js
Run Code Online (Sandbox Code Playgroud)
在哪里
-i 标志告诉 sed 就地编辑文件。编辑:感谢 User112638726 和 don_crissti,错误很明显
sed -i index 's/API_CONTEXT_URI/http:\\/\\/localhost:5557/g' www/index.js
Run Code Online (Sandbox Code Playgroud)
将被 sed 解释为
-i 就地编辑,index即i(插入)ndex,'s/API_CONTEXT_URI/http:\\/\\/localhost:5557/g'和www/index.js。我假设,没有名为 的文件s/API_CONTEXT_URI/http:\\/\\/localhost:5557/g,即当前目录g中目录http:\\/\\/localhost:5557中目录API_CONTEXT_URI中目录s中的文件。
我总是使用-e command,以防万一我需要放两个,我发现 sed 可以处理(现在)一个命令,我不确定过去是否是这种情况。