【发布时间】:2012-04-15 17:47:20
【问题描述】:
更新:
使用 sed,我如何在每个文件的关键字的第一个匹配项上插入(不替换)新行。
目前我有以下内容,但这会插入包含匹配关键字的每一行,我希望它只为文件中找到的第一个匹配项插入新插入的行:
sed -ie '/Matched Keyword/ i\New Inserted Line' *.*
例如:
我的文件.txt:
Line 1
Line 2
Line 3
This line contains the Matched Keyword and other stuff
Line 4
This line contains the Matched Keyword and other stuff
Line 6
改为:
Line 1
Line 2
Line 3
New Inserted Line
This line contains the Matched Keyword and other stuff
Line 4
This line contains the Matched Keyword and other stuff
Line 6
【问题讨论】:
-
this question 的可能重复项 - 您可以通过使用换行符和反向引用来调整它。另见this sed guide