JohnnyKing94
JohnnyKing94

Reputation: 61

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. 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"

Upvotes: 1

Views: 95

Answers (1)

anubhava
anubhava

Reputation: 785146

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.

Upvotes: 3

Related Questions