如何使用 sed 在匹配模式后添加新的文本行?

How to use sed to add a new line of text after matched pattern?

我正尝试在 Octopress 中的帖子中再添加一行。这是

comments: true

我有大约 200 个帖子,所以我想一次性完成。我正在对此进行测试,但它似乎不起作用。

echo title: 'Blah Blah.' | sed "s/'title: .*'/'title: .*'\n'comments: true'\n/g"

我要的结果是

title: 'Blah Blah.'
comments: true

这个有用吗?

echo title: 'Blah Blah.' | sed "s/'title: .*'/'\r\ncomments: true'\n/g"

@BigOldTree 的建议:

echo title: 'Blah Blah.' | sed "/title: .*/a comments: true"

awk 助你一臂之力! (作为 sed 的替代品,它可能更适合此任务)

在模式后的下一行打印

$ awk '1; /pattern/{print "comments: true"}' file

在模式的前一行打印

$ awk '/pattern/{print "comments: true"} 1' file

在同一行图案上打印

$ awk '/pattern/{print [=12=], "comments: true"; next} 1'