Insert multiple lines of text before specific line using Bash

后端 未结 9 1478
猫巷女王i
猫巷女王i 2020-12-16 03:01

I am trying to insert a few lines of text before a specific line, but keep getting sed errors when I try to add a new line character. My command looks like:



        
9条回答
  •  无人及你
    2020-12-16 03:18

    When the lines to be inserted are the result of some command "mycmd" (like cat results.txt or printf "%s\n" line{1..3}), you can do

    sed -i 's/Line to insert after/r' <(cmd) file
    or 
    sed -i 's/Line to insert after/echo "&";cmd/e' file
    

    The last command can be simple modified when you want to insert before some match.

提交回复
热议问题