1

I was trying to search for the string "BCR" in any file under Dropbox folder this manner :

grep -E -rnw "*BCR*" ~/Dropbox

but I got the following error :

grep: repetition-operator operand invalid

Is there a kind of redundancy in what the command I wrote ? I'm using bash-3.2 under Mac OSX El Capitan. Thanks in advance!

  • The problem is that your regex starts with * which is a repetition operator but nothing precedes it hence the error. – don_crissti Oct 4 '16 at 13:34
  • 1
    Well, it's actually more complicated than that... For the record, in a BRE if the 1st char is * it loses its special meaning and it matches a literal * - in a ERE it depends on the implementation e.g. OSX grep spits an error while GNU grep silently ignores it... Because you invoked grep with -E (means ERE) and because you're on OSX you get the above error... – don_crissti Oct 4 '16 at 14:32
3

Expanding on don_crissti's comment, the * operator in a regular expression needs an operand; in your case, . which stands for "any character":

grep -E -rnw ".*BCR.*" ~/Dropbox

You can get the same result without using a regular expression:

grep -rn BCR ~/Dropbox

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.