Delete all occurences of any word matching a partial string in sed

2

Assume that I have the following line:

group blue:green yellow green redtomblue black !greyTOMwhilte !purple redblue

I need to delete from it all words that contain the string "tom" or "TOM" using sed.

I have managed to get this far:

sed 's/^ *\(.*\)[!]*[A-Za-z0-9_]*[tT]\{1\}[oO]\{1\}[mM]\{1\}[A-Za-z0-9_]*\(.*\)/\1 \2/g'

But this only deletes the last occurrence, not all of them. Using my example above the result is:

group blue:green yellow green redtomblue black !grey  !purple redblue

How can I use sed to remove any word containing either of the specific substrings "tom" or "TOM"?

Sims

Posted 2011-07-18T08:44:32.573

Reputation: 21

Answers

2

What about this one?

sed 's/\S*\(tom\|TOM\)\S*//g'

resp. in non-GNU versions of sed this should work:

sed 's/[a-zA-Z0-9_!]*[Tt][Oo][Mm][a-zA-Z0-9_!]*//g'

This will substitute all words (i.e. strings beginning with 0 or more non-whitespace characters (\S) followed by tom or TOM and again 0 or more non-whitespace characters) with an empty string.
Probably you should also remove double spaces:

sed 's/\S*\(tom\|TOM\)\S*//g;s/\(\s\)\s*/\1/g'

bmk

Posted 2011-07-18T08:44:32.573

Reputation: 1 735

@Sims: I added another version which should work with non-GNU sed versions. – bmk – 2011-07-18T09:51:37.810