Sed命令替换字符串行

时间:2015-12-11 16:42:33

标签: bash sed

我有一个这样的文件,我需要用sed命令替换所有字符串行,例如我想在" Branglebracken"上使用sed。并替换为其他想要的文本,但我不想在其他字符串中包含此单词时替换它。我的意思是我只需要单独替换它,而不是将其包含在其他文本中时才需要替换它。所以基本上它必须以它开始和结束......

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]

输出就是这个

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]

正如您所注意到的,文字仍必须包含" Branglebracken"

1 个答案:

答案 0 :(得分:3)

您可以使用锚点将匹配仅限制为一行中的搜索模式:

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

^$将确保您只在一行中匹配Branglebracken