Matt Klaver
Matt Klaver

Reputation: 165

grep -v *string* and grep -v string creating wildly different results

grep -v mystring myfile.txt

returns ~300KB

grep -v *mystring* myfile.txt

returns ~7GB

....what am I doing wrong here?

Upvotes: 0

Views: 63

Answers (1)

Inian
Inian

Reputation: 85780

Your regular expression is wrong. By default grep takes regular expressions as argument along with the command line flags. The one you have attempted *mystring* is a shell glob expression which expands to a possible set of filenames containing the string mystring. So your grep commands becomes the following; on an assumption that you have filenames containing mystring

grep -v mystring1 foomystring2 foomystring3 myfile.txt

which could produce unexpected results depending on the contents of those files. The right way would be to use the greedy match quantifier .*

grep -v '.*mystring1.*' myfile.txt

Upvotes: 1

Related Questions