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

前端 未结 5 868
南方客
南方客 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 13:55

    For adding a newline after a pattern, you can also say:

    sed '/pattern/{G;}' filename
    

    Quoting GNU sed manual:

    G
        Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space.
    

    EDIT:

    Incidentally, this happens to be covered in sed one liners:

     # insert a blank line below every line which matches "regex"
     sed '/regex/G'
    

提交回复
热议问题