How to insert strings containing slashes with sed?

前端 未结 11 783
别那么骄傲
别那么骄傲 2020-11-22 04:44

I have a Visual Studio project, which is developed locally. Code files have to be deployed to a remote server. The only problem are the URLs they contain, which are hard-cod

相关标签:
11条回答
  • 2020-11-22 04:57

    The easiest way would be to use a different delimiter in your search/replace lines, e.g.:

    s:?page=one&:pageone:g
    

    You can use any character as a delimiter that's not part of either string. Or, you could escape it with a backslash:

    s/\//foo/
    

    Which would replace / with foo. You'd want to use the escaped backslash in cases where you don't know what characters might occur in the replacement strings (if they are shell variables, for example).

    0 讨论(0)
  • 2020-11-22 05:00

    please see this article http://netjunky.net/sed-replace-path-with-slash-separators/

    Just using | instead of /

    0 讨论(0)
  • 2020-11-22 05:05

    A very useful but lesser-known fact about sed is that the familiar s/foo/bar/ command can use any punctuation, not only slashes. A common alternative is s@foo@bar@, from which it becomes obvious how to solve your problem.

    0 讨论(0)
  • 2020-11-22 05:05

    A simplier alternative is using AWK as on this answer:

    awk '$0="prefix"$0' file > new_file

    0 讨论(0)
  • 2020-11-22 05:06

    Great answer from Anonymous. \ solved my problem when I tried to escape quotes in HTML strings.

    So if you use sed to return some HTML templates (on a server), use double backslash instead of single:

    var htmlTemplate = "<div style=\\"color:green;\\"></div>";
    
    0 讨论(0)
  • 2020-11-22 05:08

    replace.txt should be

    s/?page=/\/page\//g
    s/&//g
    
    0 讨论(0)
提交回复
热议问题