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

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'
  • @Sims: I added another version which should work with non-GNU sed versions. – bmk Jul 18 '11 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.