sed仅在首次比赛时插入 [英] sed to insert on first match only
本文介绍了sed仅在首次比赛时插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
已更新:
使用sed,如何仅在每个文件的关键字的第一个匹配项上插入(不替换)新行.
Using sed, how can I insert (NOT SUBSTITUTE) a new line on only the first match of keyword for each file.
目前,我有以下内容,但这会在包含匹配关键字的每一行中插入,并且我希望它仅针对文件中找到的第一个匹配项插入新插入行":
Currently I have the following but this inserts for every line containing Matched Keyword and I want it to only insert the New Inserted Line for only the first match found in the file:
sed -ie '/Matched Keyword/ i\New Inserted Line' *.*
例如:
Myfile.txt:
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 *:
If you want one with sed*:
sed '0,/Matched Keyword/s//Matched Keyword\nNew Inserted Line/' myfile.txt
*仅适用于GNU sed
*only works with GNU sed
这篇关于sed仅在首次比赛时插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文