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"?
Best Answer
What about this one?
resp. in non-GNU versions of
sed
this should work:This will substitute all words (i.e. strings beginning with 0 or more non-whitespace characters (
\S
) followed bytom
orTOM
and again 0 or more non-whitespace characters) with an empty string.Probably you should also remove double spaces: