![将关键字替换为另一个文件的全部内容](https://linux22.com/image/199972/%E5%B0%86%E5%85%B3%E9%94%AE%E5%AD%97%E6%9B%BF%E6%8D%A2%E4%B8%BA%E5%8F%A6%E4%B8%80%E4%B8%AA%E6%96%87%E4%BB%B6%E7%9A%84%E5%85%A8%E9%83%A8%E5%86%85%E5%AE%B9.png)
我编写了一个 bash 脚本,将关键字替换为另一个文件的全部内容。这是我使用的命令:
sed -i "s/CONTENT/$(cat $pathB)/" $pathA
一份文件
Here is my introduction:
CONTENT
Thank you.
B档
I'm OOO.
I'm 10 years old.
I like play baseball
结果文件
Here is my introduction:
I'm OOO.
I'm 10 years old.
I like play baseball
Thank you.
但这会显示错误:
sed: -e expression #1, char 14: unknown option to `s'
我究竟做错了什么?
答案1
您可以使用以下r
命令附加文件内容,然后删除包含模式的行:
sed -i -e "/CONTENT/r $pathB" -e '//d' $pathA
您的命令失败的原因s
是它被扩展为:
sed 's/CONTENT/Im OOO.
Im 10 years old.
I like play baseball' $pathA