Using sed's append/change/insert without a newline

馋奶兔 提交于 2019-12-10 23:46:23

问题


I want to replace my pattern space in SED. I can do this with s/^.*$/hello world/; - but can I do it using the c command somehow - without using line breaks in my sed script? It's not entirely clear to me whether that's possible in any way.

(Same question for the a and i commands)


回答1:


If your shell is bash, here is a convenient way to use c in a one-liner:

$ seq 3 | sed $'/2/c\\\nNew Text'
1
New Text
3

This looks for any line containing 2 and changes it to New Text.

This uses bash's $'...' feature to enter a newline in a string. The newline is represented by \n. The backslash that is needed after the c is represented by \\.

The $'...' feature is also available in ksh93, zsh, mksh, and FreeBSD sh.



来源:https://stackoverflow.com/questions/39058249/using-seds-append-change-insert-without-a-newline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!