Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep with negative pattern

I am looking for a way to grep a file for a specific pattern with negative pattern in it. I have a log file witch reports units version and I want to see if there is a unit witch report version other then 26.

The closest I could get is :

cat my.log | grep -i -e "version=0x[^2][^6]"

The above return a line contain "version=0x13" but not return a line contain "version=0x23"

Is there a way to tell grep to do so ?

Thanks.

like image 892
crowd Avatar asked Dec 16 '12 11:12

crowd


People also ask

How do you grep negative?

To use negative matching in grep , you should execute the command with the -v or --invert-match flags. This will print only the lines that don't match the pattern given.

How do you invert grep?

Add the -v option to your grep command to invert the results.

How do you grep for lines that don't match?

To display only the lines that do not match a search pattern, use the -v ( or --invert-match ) option. The -w option tells grep to return only those lines where the specified string is a whole word (enclosed by non-word characters). By default, grep is case-sensitive.


2 Answers

Interpret the pattern as a perl regular expression using the -P switch:

grep -iP 'version=0x(?!26)\d\d' my.log
like image 120
Birei Avatar answered Sep 28 '22 19:09

Birei


grep -i "version=0x[0-9]\\+" my.log | grep -iv "version=0x26"
like image 25
Adam Spiers Avatar answered Sep 28 '22 20:09

Adam Spiers