Linux sed 仅在第一次匹配时插入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9970124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sed to insert on first match only
提问by SSS
UPDATED:
更新:
Using sed, how can I insert (NOT SUBSTITUTE) a new line on only the first match of keyword for each file.
使用 sed,如何仅在每个文件的关键字的第一个匹配项上插入(NOT SUBSTITUTE)新行。
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' *.*
For example:
例如:
Myfile.txt:
我的文件.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
changed to:
变成:
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
采纳答案by Squazic
If you want one with sed*:
如果你想要一个带有 sed* 的:
sed '0,/Matched Keyword/s//Matched Keyword\nNew Inserted Line/' myfile.txt
*only works with GNU sed
*仅适用于 GNU sed
回答by ghoti
You can sort of do this in GNU sed:
您可以在 GNU sed 中执行此操作:
sed '0,/Matched Keyword/s//New Inserted Line\n&/'
But it's not portable. Since portability is good, here it is in awk:
但它不便携。由于可移植性好,这里是awk:
awk '/Matched Keyword/ && !x {print "Text line to insert"; x=1} 1' inputFile
Or, if you want to pass a variable to print:
或者,如果你想传递一个变量来打印:
awk -v "var=$var" '/Matched Keyword/ && !x {print var; x=1} 1' inputFile
These both insert the text line beforethe first occurrence of the keyword, on a line by itself, per your example.
根据您的示例,这些都在关键字第一次出现之前插入文本行,单独一行。
Remember that with both sed and awk, the matched keyword is a regular expression, not just a keyword.
请记住,对于 sed 和 awk,匹配的关键字是一个正则表达式,而不仅仅是一个关键字。
UPDATE:
更新:
Since this question is also tagged bash, here's a simple solution that is pure bash and doesn't required sed:
由于这个问题也被标记为bash,这里有一个简单的解决方案,它是纯 bash 并且不需要 sed:
#!/bin/bash
n=0
while read line; do
if [[ "$line" =~ 'Matched Keyword' && $n = 0 ]]; then
echo "New Inserted Line"
n=1
fi
echo "$line"
done
As it stands, this as a pipe. You can easily wrap it in something that acts on files instead.
就目前而言,这是一个管道。您可以轻松地将它包装在对文件起作用的东西中。
回答by potong
This might work for you:
这可能对你有用:
sed -i -e '/Matched Keyword/{i\New Inserted Line' -e ':a;$q;n;ba;}' *.*
You're nearly there! Just create a loop to read from the Matched Keyword
to the end of the file.
你快到了!只需创建一个循环从Matched Keyword
文件的末尾读取。
回答by Zaheer
If you want to append a line after first match only, use AWK instead of SED as below
如果您只想在第一次匹配后追加一行,请使用 AWK 而不是 SED,如下所示
awk '{print} /Matched Keyword/ && !n {print "New Inserted Line"; n++}' myfile.txt
Output:
输出:
Line 1
Line 2
Line 3
This line contains the Matched Keyword and other stuff
New Inserted Line
Line 4
This line contains the Matched Keyword and other stuff
Line 6