linux - sed 将带空格的行插入特定行

标签 linux bash sed

我在开头有一行空格,例如“Hello world”。 我想将此行插入文件中的特定行。 例如 在下一个文件中插入“hello world”

hello
world

结果:

hello
    hello world
world

我正在使用这个 sed 脚本:

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

问题是我的新行没有空格:

hello
hello world
world

最佳答案

可以对空格字符进行转义,例如添加2个空格:

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

或者您可以在 text 变量的定义中执行此操作:

text="\ \ hello world"

关于linux - sed 将带空格的行插入特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18439528/

相关文章:

linux - python将shell的输入截断为特定数字

linux - 如何为 bash 脚本中的命令模拟两次连续的 ENTER 键按下?

Bash 参数引号和 eval

linux - 使用 sed 删除一段文本

perl - 什么脚本可以帮助解析一组错误换行的行?

sed - 使用 sed 修剪文本

c++ - QBS : Module qt/core could not be loaded

python - 使用python在linux的windows 7客户端上运行python脚本

linux - 是否可以模仿 UDP 数据包的源/目标 IP?

bash - 仅使用 awk 在两个正则表达式之间打印