How to grep, excluding some patterns?

Loom picture Loom · Aug 27, 2013 · Viewed 266.9k times · Source

I'd like find lines in files with an occurrence of some pattern and an absence of some other pattern. For example, I need find all files/lines including loom except ones with gloom. So, I can find loom with command:

grep -n 'loom' ~/projects/**/trunk/src/**/*.@(h|cpp)

Now, I want to search loom excluding gloom. However, both of following commands failed:

grep -v 'gloom' -n 'loom' ~/projects/**/trunk/src/**/*.@(h|cpp)
grep -n 'loom' -v 'gloom' ~/projects/**/trunk/src/**/*.@(h|cpp)

What should I do to achieve my goal?

EDIT 1: I mean that loom and gloom are the character sequences (not necessarily the words). So, I need, for example, bloomberg in the command output and don't need ungloomy.

EDIT 2: There is sample of my expectations. Both of following lines are in command output:

I faced the icons that loomed through the veil of incense.

Arty is slooming in a gloomy day.

Both of following lines aren't in command output:

It’s gloomyin’ ower terrible — great muckle doolders o’ cloods.

In the south west round of the heigh pyntit hall

Answer

houbysoft picture houbysoft · Aug 27, 2013

How about just chaining the greps?

grep -n 'loom' ~/projects/**/trunk/src/**/*.@(h|cpp) | grep -v 'gloom'