sed错误的原因是什么?

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.jswww的文件夹。我曾尝试更改\\/\escape /,但仍然无法正常工作。大家能帮我看看哪里吗?我对命令中的-i和 表示怀疑index

Arc*_*mar 5

  1. -es/foo/bar/ (*) 之前缺少一个
  2. 有一个困惑,你是(脚本)编辑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 就地编辑,
  • indexi(插入)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 可以处理(现在)一个命令,我不确定过去是否是这种情况。

  • 不。在这种情况下,_1._ `-e` 是多余的,因为您只使用一个 `sed` _expression_。_2._ @User112638726 的评论具有误导性。`-i` 之后的 `index` 应该是备份文件的后缀,确实如此,但前提是中间没有空格。因此,`-i index` 被解释为使用表达式_`index` 就地编辑文件(不备份) - 请参阅我在问题下的评论......所以这个问题的原因是单个空格字符。它必须被删除,例如`-iindex` 或`-i.index` 等...以使命令工作。 (2认同)