How to find string and delete before just in line?

Hello,

When my lines contain question mark, I use below command to delete the portion of the matching line coming after question mark:

sed 's/?.*//' SampleFile

SampleFile:

helloworldfirstline?mdksmyymsss
hellosecondlineworld?mdksmkkmsss
thirdhelloworld?mdksmccmsss

Output:

helloworldfirstline
hellosecondlineworld
thirdhelloworld

What if I wish to delete before the question mark in each line?

Expected output1:

mdksmyymsss
mdksmkkmsss
mdksmccmsss

or ...

Expected output2:

mdksmyymsss?
mdksmkkmsss?
mdksmccmsss?

With or without question mark, both are welcome.

Many Thanks
Boris

echo 'helloworldfirstline?mdksmyymsss' | sed 's/.*?//'
or
echo 'helloworldfirstline?mdksmyymsss' | sed 's/[^?]*//'
2 Likes

Many thanks vgersh99,
First one (without question mark) works well at my end.

Kind regards
Boris