How grep excluding some patterns?

I would like to find lines in files with the appearance of some pattern and the absence of any other pattern. For example, I need to find all files / lines, including loom , except those that have gloom . So, I can find loom with the command:

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

Now I want to search for loom except gloom . However, both of the following commands were not executed:

 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 sequences of characters (not necessarily words). Therefore, I need, for example, bloomberg to output the command and not need ungloomy .

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

I came across icons that loomed through the veil of incense.

Artie falls asleep on a gloomy day.

Both of the following lines are not output by the command:

Its terrible marble horror - the great tricks of o cloods.

In the southwestern circle of the hip paint hall

+65
bash grep
Aug 27 '13 at 14:47
source share
10 answers

How to just tie greps?

 grep -n 'loom' ~/projects/**/trunk/src/**/*.@(h|cpp) | grep -v 'gloom' 
+82
Aug 27 '13 at 14:49
source share

Another solution without grep binding:

 egrep '(^|[^g])loom' ~/projects/**/trunk/src/**/*.@(h|cpp) 

Between the brackets, you exclude the character g before any occurrence of loom , unless loom is the first character of the string.

+15
Aug 27 '13 at 15:00
source share

A bit old, but good ...

The most popular solution from @houbysoft will not work, since it excludes any line with a “gloom” in it, even if it has a “loom”. According to OP's expectations, we should include “loom” strings, even if they also have “gloom” in them. This line should be in the output “Arty is a dark day hurricane”, but this will be ruled out by chained grep like

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

Instead, egrep regex Bentoy13 example works better

 egrep '(^|[^g])loom' ~/projects/**/trunk/src/**/*.@(h|cpp) 

since it will include any string with a “weaving station” in it, regardless of whether it has a “gloom”. On the other hand, if he is just depressed, he will not turn it on, namely what the OP wants.

+8
Jul 08 '16 at 7:59
source share

Just use awk, it is much easier than grep, allowing you to clearly express complex conditions.

If you want to skip lines containing both loom and gloom :

 awk '/loom/ && !/gloom/{ print FILENAME, FNR, $0 }' ~/projects/**/trunk/src/**/*.@(h|cpp) 

or if you want to print them:

 awk '/(^|[^g])loom/{ print FILENAME, FNR, $0 }' ~/projects/**/trunk/src/**/*.@(h|cpp) 

and if reality is just lines where loom appears as the word itself:

 awk '/\<loom\>/{ print FILENAME, FNR, $0 }' ~/projects/**/trunk/src/**/*.@(h|cpp) 
+7
Aug 28 '13 at 3:42 on
source share

-v is the “inverted match” flag, so piping is a very good way:

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

+6
May 09 '18 at 12:01
source share

/ * Perhaps you look something like this?

 grep -vn "gloom" `grep -l "loom" ~/projects/**/trunk/src/**/*.@(h|cpp)` 

BACKQUOTES are used as brackets for commands, so in this case with -l turned on, the code in BACKQUOTES will return the file names to you, and then -vn will do everything you need: have the file names, sheets, and also the actual lines.

UPDATE Or using xargs

 grep -l "loom" ~/projects/**/trunk/src/**/*.@(h|cpp) | xargs grep -vn "gloom" 

Hope this helps. * /

Please ignore what I wrote above, this is trash.

 grep -n "loom" `grep -l "loom" tt4.txt` | grep -v "gloom" #this part gets the filenames with "loom" #this part gets the lines with "loom" #this part gets the linenumber, #filename and actual line 
+5
Aug 27 '13 at 15:47
source share

You can use grep -P (perl regex) supported by negative lookbehind :

 grep -P '(?<!g)loom\b' ~/projects/**/trunk/src/**/*.@(h|cpp) 

I added \b for word boundaries.

+4
Aug 27 '13 at 14:58
source share
 grep -n 'loom' ~/projects/**/trunk/src/**/*.@(h|cpp) | grep -v 'gloom' 
+3
Aug 27 '13 at 14:49
source share

Question: Find the “loom”, excluding the “gloom”.
Answer:

 grep -w 'loom' ~/projects/**/trunk/src/**/*.@(h|cpp) 
0
Jun 27. '17 at 13:59 on
source share

Just use it! grep -v several times.

File content

 [root@server]# cat file 1 2 3 4 5 

Exclude string or match

 [root@server]# cat file |grep -v 3 1 2 4 5 

Exclude string or match multiple

 [root@server]# cat file |grep -v 3 |grep -v 5 1 2 4 
0
Apr 15 '19 at 8:09
source share



All Articles