我想在任何现有的#include之前,用一个额外的include指令更新大量的c++源文件。对于这类任务,我通常使用带有sed的小型bash脚本来重写文件。
我如何得到sed替换一个文件中字符串的第一次出现,而不是替换每一次出现?
如果我用
sed s/#include/#include "newfile.h"\n#include/
它替换所有#includes。
我们也欢迎为达到同样目的而提出的其他建议。
我想在任何现有的#include之前,用一个额外的include指令更新大量的c++源文件。对于这类任务,我通常使用带有sed的小型bash脚本来重写文件。
我如何得到sed替换一个文件中字符串的第一次出现,而不是替换每一次出现?
如果我用
sed s/#include/#include "newfile.h"\n#include/
它替换所有#includes。
我们也欢迎为达到同样目的而提出的其他建议。
当前回答
下面的命令删除文件中字符串的第一次出现。它还删除了空行。它显示在xml文件上,但它可以用于任何文件。
如果您使用xml文件并且想要删除一个标记,则非常有用。在本例中,它删除了第一次出现的“isTag”标记。
命令:
sed -e 0,/'<isTag>false<\/isTag>'/{s/'<isTag>false<\/isTag>'//} -e 's/ *$//' -e '/^$/d' source.txt > output.txt
源文件:Source .txt
<xml>
<testdata>
<canUseUpdate>true</canUseUpdate>
<isTag>false</isTag>
<moduleLocations>
<module>esa_jee6</module>
<isTag>false</isTag>
</moduleLocations>
<node>
<isTag>false</isTag>
</node>
</testdata>
</xml>
结果文件(output.txt)
<xml>
<testdata>
<canUseUpdate>true</canUseUpdate>
<moduleLocations>
<module>esa_jee6</module>
<isTag>false</isTag>
</moduleLocations>
<node>
<isTag>false</isTag>
</node>
</testdata>
</xml>
ps:它在Solaris SunOS 5.10(相当旧)上不能工作,但在Linux 2.6 sed版本4.1.5上可以工作
其他回答
以下是许多有用的现有答案的概述,并附有解释:
这里的例子使用了一个简化的用例:只在第一个匹配的行中将单词'foo'替换为'bar'。 由于使用ANSI c引号字符串($'…')来提供示例输入行,bash、ksh或zsh被假定为shell。
GNU仅适用:
Ben Hoffstein的回答告诉我们,GNU为sed提供了POSIX规范的扩展,它允许以下2-address形式:0,/re/ (re在这里表示任意正则表达式)。
0,/re/也允许正则表达式匹配第一行。换句话说:这样的地址将创建一个范围,从第一行到匹配re的行,无论re出现在第一行还是任何后续行。
与posix兼容的表单1 /re/形成对比,该表单创建了一个范围,从第一行匹配到后续行中与re匹配的行;换句话说:这将不会检测第一次出现的re匹配,如果它恰好出现在第一行,并且还防止使用简写//来重用最近使用的正则表达式(参见下一点).1
如果将0,/re/地址与s/…/…/(替换)调用使用相同的正则表达式,您的命令将有效地只对匹配re的第一行执行替换。 Sed为重用最近应用的正则表达式提供了一种方便的快捷方式:空分隔符对//。
$ sed '0,/foo/ s//bar/' <<<$'1st foo\nUnrelated\n2nd foo\n3rd foo'
1st bar # only 1st match of 'foo' replaced
Unrelated
2nd foo
3rd foo
只支持posix特性的sed,例如BSD (macOS) sed(也适用于GNU sed):
由于0,/re/不能使用,并且形式1,/re/将不会检测到re,如果它恰好出现在第一行(见上文),因此需要对第一行进行特殊处理。
MikhailVS的回答提到了这种技术,并举了一个具体的例子:
$ sed -e '1 s/foo/bar/; t' -e '1,// s//bar/' <<<$'1st foo\nUnrelated\n2nd foo\n3rd foo'
1st bar # only 1st match of 'foo' replaced
Unrelated
2nd foo
3rd foo
注意:
The empty regex // shortcut is employed twice here: once for the endpoint of the range, and once in the s call; in both cases, regex foo is implicitly reused, allowing us not to have to duplicate it, which makes both for shorter and more maintainable code. POSIX sed needs actual newlines after certain functions, such as after the name of a label or even its omission, as is the case with t here; strategically splitting the script into multiple -e options is an alternative to using an actual newlines: end each -e script chunk where a newline would normally need to go.
1s /foo/bar/只在第一行替换foo,如果在第一行找到的话。 如果是,t将分支到脚本的末尾(跳过该行中剩余的命令)。(t函数只有在最近的s调用执行了实际的替换时才会分支到一个标签;在没有标签的情况下,就像这里的情况一样,脚本的结尾被分支到)。
当这种情况发生时,范围地址1,//(通常从第2行开始查找第一个出现的地址)将不匹配,并且该范围将不会被处理,因为当当前行已经是2时,该地址将被计算。
相反,如果第一行没有匹配项,则输入1,//,并找到真正的第一个匹配项。
最终效果与GNU sed的0,/re/相同:只替换第一个出现的内容,无论它出现在第一行还是其他任何一行。
NON-range方法
Potong的回答展示了绕过范围需求的循环技术;因为他使用GNU sed语法,这里是posix兼容的等价:
循环技术1:在第一次匹配时,执行替换,然后输入一个循环,只打印剩余的行:
$ sed -e '/foo/ {s//bar/; ' -e ':a' -e '$!{n;ba' -e '};}' <<<$'1st foo\nUnrelated\n2nd foo\n3rd foo'
1st bar
Unrelated
2nd foo
3rd foo
循环技术2,仅适用于较小的文件:将整个输入读入内存,然后对其执行一次替换。
$ sed -e ':a' -e '$!{N;ba' -e '}; s/foo/bar/' <<<$'1st foo\nUnrelated\n2nd foo\n3rd foo'
1st bar
Unrelated
2nd foo
3rd foo
1 1.61803提供了1,/re/,带和不带后面的s//的例子:
sed '1,/foo/ s/foo/bar/' <<<$'1foo\n2foo' yields $'1bar\n2bar'; i.e., both lines were updated, because line number 1 matches the 1st line, and regex /foo/ - the end of the range - is then only looked for starting on the next line. Therefore, both lines are selected in this case, and the s/foo/bar/ substitution is performed on both of them. sed '1,/foo/ s//bar/' <<<$'1foo\n2foo\n3foo' fails: with sed: first RE may not be empty (BSD/macOS) and sed: -e expression #1, char 0: no previous regular expression (GNU), because, at the time the 1st line is being processed (due to line number 1 starting the range), no regex has been applied yet, so // doesn't refer to anything. With the exception of GNU sed's special 0,/re/ syntax, any range that starts with a line number effectively precludes use of //.
# sed script to change "foo" to "bar" only on the first occurrence
1{x;s/^/first/;x;}
1,/foo/{x;/first/s///;x;s/foo/bar/;}
#---end of script---
或者,如果你喜欢:编辑注:只适用于GNU sed。
sed '0,/foo/s//bar/' file
源
这可能为你工作(GNU sed):
sed -si '/#include/{s//& "newfile.h\n&/;:a;$!{n;ba}}' file1 file2 file....
或者如果内存不是问题:
sed -si ':a;$!{N;ba};s/#include/& "newfile.h\n&/' file1 file2 file...
你可以使用awk来做类似的事情。
awk '/#include/ && !done { print "#include \"newfile.h\""; done=1;}; 1;' file.c
解释:
/#include/ && !done
当行匹配“#include”并且我们还没有处理它时,在{}之间运行操作语句。
{print "#include \"newfile.h\""; done=1;}
这将打印#include "newfile.h",我们需要转义引号。然后我们将done变量设置为1,这样我们就不会添加更多的include。
1;
这意味着“打印出行”——一个空操作默认为打印$0,打印出整行。一个简单的程序,比sed更容易理解:-)
POSIXly(在sed中也有效),只使用一个正则表达式,只需要一行内存(和往常一样):
sed '/\(#include\).*/!b;//{h;s//\1 "newfile.h"/;G};:1;n;b1'
解释道:
sed '
/\(#include\).*/!b # Only one regex used. On lines not matching
# the text `#include` **yet**,
# branch to end, cause the default print. Re-start.
//{ # On first line matching previous regex.
h # hold the line.
s//\1 "newfile.h"/ # append ` "newfile.h"` to the `#include` matched.
G # append a newline.
} # end of replacement.
:1 # Once **one** replacement got done (the first match)
n # Loop continually reading a line each time
b1 # and printing it by default.
' # end of sed script.