Using SED to extract a word or string.

I am working with the ksh shell in HP UNIX and I am attempting to extract a word, beginning with a particular string and ending at the first space. for example I want to extract the word or string MS_RECENT_ACTIVITY from the following string

" This has been entered in MS_RECENT_ACTIVITY the table that was last "

I have tried to use the SED string with the following

echo " This has been entered in MS_RECENT_ACTIVITY the table that was last " |sed -n 's/.*\([Mm][Ss]_.*\).*/\1/p'

The results are

How can I only get "MS_RECENT_ACTIVITY" extracted always starting with the " MS_" and ending at the first following blank or space using SED? The wording following will not be constant as the string will not always be "the table that..." I am attempting to avoid using a pipe to awk to print the first word.

Thank you for your assistance.

Try:

sed -n 's/^.*\([Mm][Ss]_[^ ]*\).*/\1/p'

That code works perfect. Thank you so much.:wink: