How do I insert a newline/linebreak after a line using sed

前端 未结 5 862
南方客
南方客 2020-12-28 13:43

It took me a while to figure out how to do this, so posting in case anyone else is looking for the same.

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-28 14:15

    A simple substitution works well:

    sed 's/pattern.*$/&\n/'
    

    Example :

    $ printf "Hi\nBye\n" | sed 's/H.*$/&\nJohn/'
    Hi
    John
    Bye
    

    To be standard compliant, replace \n by backslash newline :

    $ printf "Hi\nBye\n" | sed 's/H.*$/&\
    > John/'
    Hi
    John
    Bye
    

提交回复
热议问题