1

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"

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, didn't know about anchors with sed... I learn something new every day ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.