【发布时间】:2018-11-01 19:40:15
【问题描述】:
我在尝试用 sed 进行以下替换时迷路了:
@编辑:捕捉我的问题的全部复杂性, 我后来添加了文件名包含在变量中的事实。 因此,解决方案可能会直接使用文件名。
给定一个变量 I='insert.txt':
'插入.txt':
Text I wanna skip.
This is text to insert containing
spaces and new lines
给定一个变量 M='toModify.txt':
'toModify.txt':
Insert the new text: here.
我想用内容替换 $M 中的“这里” $I:
Insert the new text: This is text to insert containing
spaces and new lines.
我试过了:
sed -e "s/here/$(tail -n2 $I | sed -e 's/ /\\ /g' | tr '\n' '\\n')/" $M
有错误: sed 未终止的 `s' 命令
问题是在不终止 s 命令的情况下我没有得到空格和换行符。
有什么办法吗?
【问题讨论】:
-
请将该示例输入的所需输出添加到您的问题中。
-
非常类似于Use the contents of a file to replace a string using SED。有一个很好的区别,但这个答案可能会对你有所帮助。
-
在
insert.txt中,如何区分要跳过的文本和要插入的文本?