This turned out to be harder than I thought:
How can I extract what a pattern matches from a line,
i.e., replace the line with the part that matches the pattern?
echo "this is a sample line" | sed -n "s/.*\(s[a-z]*e\).*/\1/p"
prints "sample" in this case but will not work in the general
case, e.g., replace "sample" with "ssample" and the first s
will not show up. In effect, the preceding ".*" will cause
the second pattern to find last possible match instead of
the first (longest).
Replacing ".*" with something that can't match the beginning
of the target pattern works, IF you can find a suitable
substitute -- there isn't any that works in every case.
Here's one way that works, more or less:
but it's rather ugly.
Better suggestions, anyone?