Use sed command in Linux to adjust a text file that contains an email structure

Hi Community builders,

Please I want to find a way to use sed to substitute a word after a specific character and before another specific character without knowing what is the word.

Like :

Subject: Automation - This is a test build

I want it to be :

Subject: Hello - This is a test build


Taking into consideration that I don't know that the word is "Automation"

I've tried sed 's/Subject:[^ ]* .*-/Hello/g'

the output was : "Hello This is a test build"

Your assistance is highly appreciated.

What matches is substituted.
You must restore the matching portion that you want to keep.

sed 's/^Subject: [^ ]* -/Subject: Hello -/'

The following uses two capture groups \( \)
and references to the 1st and 2nd capture group \1 \2

sed 's/^\(Subject: *\)[^ ]*\( -\)/\1Hello\2/g'
1 Like

Thank you so much.

I added a . to the solution you provided which worked after

sed 's/^\(Subject: *\)[^ ].*\( -\)/\1Hello\2/g'

Thanks again