bash sed 只更改文件中的字符串一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8081297/
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 change string in a file only once
提问by lxyu
suggest I have a file exampleas follows:
建议我有一个文件example如下:
c
a
b
a
b
d
and I want to change the first occurance of ato e. Then I do this:
我想改变的第一次出现a到e。然后我这样做:
sed -i 's/a/e/' example
and all achanged to e.
并且都a变成了e。
So is there any way to make sed only replace once within a file?
那么有没有办法让 sed 在一个文件中只替换一次?
Thanks.
谢谢。
回答by Felix Rabe
Applying the information from Aziz' duplicate link to your question, I think this will give you the desired result for your case:
将 Aziz 的重复链接中的信息应用于您的问题,我认为这将为您的案例提供所需的结果:
sed -i '0,/a/s//e/' example
回答by accuya
Applying the information from Aziz' duplicate link to your question, I think this will give you the desired result for your case:
sed -i '0,/a/s//e/' example
将 Aziz 的重复链接中的信息应用于您的问题,我认为这将为您的案例提供所需的结果:
sed -i '0,/a/s//e/' example
Rabe's answer is good. But, adding a space between address range and 's' command makes it more clearer. like this:
拉贝的回答很好。但是,在地址范围和 's' 命令之间添加一个空格使其更清晰。像这样:
sed -i '0,/a/ s//e/' example
And Wilson's version seems redundant.
威尔逊的版本似乎是多余的。

