In the editor sam, one can search for and print out the string "ed" but not "red" with this command:

,x./*\n/ g/ed v/red p

I was trying to find a way to do something similar in ed. I want to search for the string "ed" but not get lines with:

red, loved, loaded, etc.

I am familiar with the v command but not sure how to combine this with a g command, or if it is possible in ed.


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 [[:alnum:]_]. The \< and \> are zero-width assertions, and will match at the border between word characters and non-word characters.

The \< and \> are non-standard, but available on GNU systems, and on some BSD systems. macOS uses [[:<:]] and [[:>:]] 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.

  • I can confirm that this solution works on Gnu ed 1.18-pre2, and on the ed that comes shipped with Solaris, but not on the Plan9 ed which I am using. It appears then that older Unix versions of ed do not support zero-width assertions. I wonder then how this effect is achieved in these older versions.
    – devcom
    Jul 28 at 20:37

Just as an extension to Kusalananda's answer, Ex is also an POSIX editor and POSIX does require it to support the \< \> word anchors. So I suggest just using


but in Ex.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.