【问题标题】:sed linux command unable to insert line [duplicate]sed linux命令无法插入行[重复]
【发布时间】:2019-01-03 17:28:43
【问题描述】:

我怎样才能使这个正则表达式正常工作。正在添加行

LOGIN_SERVICE: '"https://dev-login-o365.grey.com/gp_loginservice/"',

在文件env.js我使用的是正则表达式:

sed -i '5i LOGIN_SERVICE: '"https://login.xxxx.com/server/"',' ./env.js

但是在左边添加不带引号和 3 个空格的值,如下所示:

【问题讨论】:

    标签: regex linux sed


    【解决方案1】:

    使用不同的引用:

    sed -i "5i LOGIN_SERVICE: '\"https://login.xxxx.com/server/\"'," env.js
    

    或者按照下面有用的 cmets 的建议,将 sed 命令用单引号括起来:

    sed '5i LOGIN_SERVICE: '\''"https://login.xxxx.com/server/"'\'',' env.js
    

    【讨论】:

    • 另外,'5i LOGIN_SERVICE: '\''"https://login.xxxx.com/server/"'\'',' - 这对于程序引用很有用,因为它只是 s.replace("'", "'\\''")
    • 不要在脚本周围使用双引号,因为这样你会在 sed 甚至看到它之前将整个文本暴露给 shell 进行解释,所以如果/当你的文本包含字符时你会感到惊讶shell 将作用于(例如$)。只需像@o11c 建议的那样做sed -i '5i LOGIN_SERVICE: '\''"https://login.xxxx.com/server/"'\'',' ./env.js
    • 谢谢,Ed 和@o11c 说的很对
    猜你喜欢
    • 1970-01-01
    • 2014-10-27
    • 2015-01-29
    • 2017-05-20
    • 2016-12-03
    • 2021-03-02
    • 1970-01-01
    • 2021-12-11
    相关资源
    最近更新 更多