如何使用sed插入多行

Pau*_*ner 12 bash sed

我想添加这个

#this 
##is my 
text
Run Code Online (Sandbox Code Playgroud)

行前

the specific line 
Run Code Online (Sandbox Code Playgroud)

我试过这个

sed -i '/the specific line/i \
#this 
##is my 
text
' text.txt
Run Code Online (Sandbox Code Playgroud)

但它只添加了“文本”。

我也试图与不同的组合\" ",但毫无效果。

kos*_*kos 11

您在某些行的末尾缺少尾随反斜杠(并且您要插入的最后一行末尾有一个 eccessive 换行符):

sed -i '/the specific line/i \
#this\
##is my\
text' file
Run Code Online (Sandbox Code Playgroud)
% cat file
foo
the specific line
bar

% sed -i '/the specific line/i \
#this\
##is my\
text' file

% cat file
foo
#this 
##is my 
text
the specific line
bar
Run Code Online (Sandbox Code Playgroud)


A.B*_*.B. 6

换行符:

% sed -i '/the specific line/i #this\n##is my\ntext' foo

% cat foo
#this
##is my
text
the specific line
Run Code Online (Sandbox Code Playgroud)