Using grep to find whole word with wildcard and exclusions

Viewed 283

I have some random strings. I am trying to print only the whole words with the following:

grep -ioh "\w*ice\w*"

This works fine but it seems to don't showing the symbols but only letters. I'd like the wildcards to allow any symbols but the spaces. Is this something possible?

To better explain, the above code in a string like:

The cat is very-nice shows only "nice" while I'd like to have "very-nice"

1 Answers

You may use

grep -ioh "\S*ice\S*"

Or

grep -ioh "[^[:space:]]*ice[^[:space:]]*"

The [^[:space:]] (or \S) match any char but a whitespace char.

See the grep online demo:

grep -ioh "\S*ice\S*" <<< "The cat is very-nice"
## => very-nice
Related