如何使用sed而不是文件将字符串添加到文件中

时间:2017-12-08 07:58:46

标签: bash sed

red    
blue    
water    
gray    
white

我想用

sed '/blue/,/gray/!b;//!d;/blue/r file2' file1

正如此Replace text between two lines with contents of a file stored in a variable in sed主题

中所述

但是我想直接在命令中输入新的字符串而不是file2。任何人都可以告诉我如何做到这一点。

1 个答案:

答案 0 :(得分:1)

您可以使用此sed

sed '/blue/,/gray/!b;//!d;/blue/a SOMEDATA' file

来自man sed

a \
  text

附加文本,每个嵌入的换行符都以反斜杠开头。

另一个例子:

$ sed '/blue/,/gray/!b;//!d;/blue/a \
> LINE 1 \
> LINE 2 \
> LINE 3' file

red    
blue    
LINE 1 
LINE 2 
LINE 3
gray    
white