sed将空格插入到特定行

我在开始的时候有一个空格,例如“Hello world”。 我想插入这一行到文件中的特定行。 例如插入“hello world”到下一个文件

hello world 

结果:

 hello hello world world 

我正在使用这个sed脚本:

 sed -i "${line} i ${text}" $file 

问题是,我得到了我的新行空间:

 hello hello world world 

您可以转义space字符,例如添加2个空格:

 sed -i "${line} i \ \ ${text}" $file 

或者你可以在你的text变量的定义中做到这一点:

 text="\ \ hello world" 
 $ a=" some string " $ echo -e "hello\nworld" hello world $ echo -e "hello\nworld" | sed "/world/ s/.*/${a}.\n&/" hello some string . world 

. 被添加在上面的替代中以证明尾随的白色面被保留。 请改用sed "/world/ s/.*/${a}\n&/"

你只需要一个\来输入多个这样的空白

 sed -i "${line} i \ ${text}" $file