Bash 在正文中替换段落

Bash 在正文中替换段落,bash,awk,sed,Bash,Awk,Sed,如何使用shell命令(sed或awk)替换文件中的段落,如下所示: bala-bala-ba1 巴拉巴拉。 把我留在这里。 本段 是的,我要去 取而代之 其余的包含 不应该被替换。 更换后: bala-bala-ba1 巴拉巴。 把我留在这里。 新内容, 我是替补! 其余的包含 不应该被替换。 我声明原始文件包含在变量$OLD中,新文件包含在另一个变量$new中,假设您有包含新旧段落的变量,如 $ echo "$old" this paragraph yes, I'm going to b

如何使用shell命令(sed或awk)替换文件中的段落,如下所示:

bala-bala-ba1
巴拉巴拉。
把我留在这里。
本段
是的,我要去
取而代之
其余的包含
不应该被替换。
更换后:

bala-bala-ba1
巴拉巴。
把我留在这里。
新内容,
我是替补!
其余的包含
不应该被替换。

我声明原始文件包含在变量
$OLD
中,新文件包含在另一个变量
$new
中,假设您有包含新旧段落的变量,如

$ echo "$old"
this paragraph
yes, I'm going
to be replaced

您可以将该文件(假定其名为
infle
)读入Bash变量并使用参数展开:

$ contents=$(< infile)
$ echo "${contents/$old/$new}"
bala bala ba1
balabala.
leave me here.

New contains,
I'm replacement!

The rest contains
should not be replaced.

假设您有包含新旧段落的变量,如

$ echo "$old"
this paragraph
yes, I'm going
to be replaced

您可以将该文件(假定其名为
infle
)读入Bash变量并使用参数展开:

$ contents=$(< infile)
$ echo "${contents/$old/$new}"
bala bala ba1
balabala.
leave me here.

New contains,
I'm replacement!

The rest contains
should not be replaced.

对谢谢你,你救了我一天!对谢谢你,你救了我一天!