【问题标题】:Sed command replace string linesed 命令替换字符串行
【发布时间】:2015-12-11 16:42:33
【问题描述】:

我有一个像这样的文件,我需要用 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”一词

【问题讨论】:

    标签: bash sed


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢,不知道有 sed 的主播...我每天都学到新东西 ;)
    猜你喜欢
    • 1970-01-01
    • 2021-05-16
    • 2022-10-24
    • 2021-10-28
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 2017-09-25
    相关资源
    最近更新 更多