Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"?

share|improve this question

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'
share|improve this answer
    
@Sims: I added another version which should work with non-GNU sed versions. – bmk Jul 18 '11 at 9:51
    
Worked, thanks a lot!!!! – Sims Jul 18 '11 at 14:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .