0

Trying to get only words that come before "/", I wrote:

T='He/She is a very handsome/beautiful man/woman indeed.'
echo "$T" | sed -E 's#\b(.*)/(.*)\b#\1#g'

However, I only got to make it work in the last occurrence (although I'm using "g" in sed sentence):

He/She is a very handsome/beautiful man.

My desired output is: "He is a very handsome man indeed."

Any ideas? Thank you.

2 Answers 2

3

A POSIX compliant one:

T='He/She is a very handsome/beautiful man/woman indeed.'
echo "$T" | sed 's:/[^ ]*::g'
Sign up to request clarification or add additional context in comments.

Comments

2

To remove non-space characters after every slash you can use:

$ sed 's:/\S*::g' <<<'He/She is a very handsome/beautiful man/woman indeed.'
He is a very handsome man indeed.

The pattern, :/\S*:, matches a slash followed by zero or more non-space characters. The replacement string, ::, is empty and it is applied globally g. The <<< is a here-string that passes input to sed.

2 Comments

Can you help me understand this regex?
The basic \S is the way to go. You can also use the extended regex \w (word chars)to the same effect, though not all seds support extended regex. E.g. sed -E 's:/\w*::g' The word chars are equivalent to [:alnum:] + '_' (e.g. sed 's:/[[:alnum:]_]*::g').

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.