简体   繁体   English

Sed命令替换字符串行

[英]Sed command replace string line

I have a file like this where i need to replace all the strings line with sed command, for example i want to use sed on "Branglebracken" and to replace with other wanted text, but i do not want to replace it when this word is included in other strings. 我有一个这样的文件,我需要用sed命令替换所有字符串行,例如我想在“Branglebracken”上使用sed并替换为其他想要的文本,但我不想替换它时这个词是包含在其他字符串中。 I mean i need to replace it only when the word is only alone, not when it's included in other texts. 我的意思是我只需要单独替换它,而不是将其包含在其他文本中时更换它。 So basically it must start and end with it... 所以基本上它必须以它开始和结束......

I came here to Branglebracken with my wife and child, as well as an Altmer herbalist who wanted to explore the forest.\n\nCould you check on them? Since they're not back already, I assume they've taken a rest at the wayshrine to the northwest.
Branglebracken
Branglebracken
Branglebracken
[intentionally repeated]

The output is this 输出就是这个

I came here to Branglebracken with my wife and child, as well as an Altmer herbalist who wanted to explore the forest.\n\nCould you check on them? Since they're not back already, I assume they've taken a rest at the wayshrine to the northwest.
word-replaced
word-replaced
word-replaced
[intentionally repeated]

As you notice, the text must still have the word "Branglebracken" 正如您所注意到的,该文本仍必须包含“Branglebracken”一词

You can use anchors to restrict your match to only that search pattern in a line: 您可以使用锚点将匹配仅限制为一行中的搜索模式:

sed 's/^Branglebracken$/word-replaced/' file

^ and $ will ensure you only match Branglebracken in a line. ^$将确保您只匹配Branglebracken

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM