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

前端 未结 5 860
南方客
南方客 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:18

    This sed command:

    sed -i '' '/pid = run/ a\
    \
    ' file.txt
    

    Finds the line with: pid = run

    file.txt before

    ; Note: the default prefix is /usr/local/var
    ; Default Value: none
    ;pid = run/php-fpm.pid
    
    ; Error log file
    

    and adds a linebreak after that line inside file.txt

    file.txt after

    ; Note: the default prefix is /usr/local/var
    ; Default Value: none
    ;pid = run/php-fpm.pid
    
    
    ; Error log file
    

    Or if you want to add text and a linebreak:

    sed -i '/pid = run/ a\
    new line of text\
    ' file.txt
    

    file.txt after

    ; Note: the default prefix is /usr/local/var
    ; Default Value: none
    ;pid = run/php-fpm.pid
    new line of text
    
    ; Error log file
    

提交回复
热议问题