This is better done with a regular expression that includes word boundary markers:
Note that I'm not entirely familiar with
sam so I can't comment on the editing expression that you're showing. The expression above would print each line containing the word
ed, i.e. the string
ed preceded and followed by something other than a word character. A word character is a character from the set
\> are zero-width assertions, and will match at the border between word characters and non-word characters.
\> are non-standard, but available on GNU systems, and on some BSD systems. macOS uses
[[:>:]] instead while GNU systems additionally may use
\b both at the start and end of a word to match.
If you want to do this portably, you will have to match
[^[:alnum:]_]ed[^[:alnum:]_] but also remember to account for the special cases where the word occurs at the very start or end of a line, or where
ed are the only two characters on a line.