Using different delimiters in sed commands and range addresses

前端 未结 1 1436
无人共我
无人共我 2020-11-21 06:43

I am using sed in a shell script to edit filesystem path names. Suppose I want to replace

/foo/bar

with

/baz/qux
相关标签:
1条回答
  • 2020-11-21 07:42

    You can use an alternative regex delimiter as a search pattern by backslashing it:

    sed '\,some/path,d'
    

    And just use it as is for the s command:

    sed 's,some/path,other/path,'
    

    You probably want to protect other metacharacters, though; this is a good place to use Perl and quotemeta, or equivalents in other scripting languages.

    From man sed:

    /regexp/
    Match lines matching the regular expression regexp.

    \cregexpc
    Match lines matching the regular expression regexp. The c may be any character other than backslash or newline.

    s/regular expression/replacement/flags
    Substitute the replacement string for the first instance of the regular expression in the pattern space. Any character other than backslash or newline can be used instead of a slash to delimit the RE and the replacement. Within the RE and the replacement, the RE delimiter itself can be used as a literal character if it is preceded by a backslash.

    0 讨论(0)
提交回复
热议问题