更新:
使用sed,如何在每个文件的关键字的第一个匹配项上插入(不替换)新行。
目前,我有以下内容,但此代码会插入包含匹配关键字的每一行,并且我希望它仅针对文件中找到的第一个匹配项插入“新插入行”:
sed -ie '/Matched Keyword/ i\New Inserted Line' *.*
例如:
Myfile.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
如果要使用sed *:
sed '0,/Matched Keyword/s//Matched Keyword\nNew Inserted Line/' myfile.txt
*仅适用于GNU sed