escaping newlines in sed replacement string

前端 未结 5 581
遥遥无期
遥遥无期 2020-12-09 09:05

Here are my attempts to replace a b character with a newline using sed while running bash

$> echo \'abc\' | sed \'s         


        
5条回答
  •  一整个雨季
    2020-12-09 09:25

    $ echo 'abc' | sed 's/b/\'$'\n''/'
    a
    c
    

    In Bash, $'\n' expands to a single quoted newline character (see "QUOTING" section of man bash). The three strings are concatenated before being passed into sed as an argument. Sed requires that the newline character be escaped, hence the first backslash in the code I pasted.

提交回复
热议问题