Issue
I have been using grep
to perform recursive search of text inside a directory, i.e.
grep -Hrn "some-text" ./
However, I am running into some troubles when I need to search for pointers or pointers of pointers:
grep -Hrn "double**" ./
> grep: repetition-operator operand invalid
I have made some attempts to go around this, including some I found via Google searches:
grep -Hrn "double[**]" ./
grep -Hrn "double[*][*]" ./
but neither seems to work. Any pointers?
Solution
You have to escape *
by using \
. For example
$ echo "double***" | grep "double\*\*\*"
double***
If you don't escape *
it matches the character before the *
zero or more times. One *
would therefore match e.g. doubleeeee
but the second *
results in an error since the operand (the character before the *
) is not valid since it's again *
. That's exactly what the error message tells you.
The version using []
should also work. As mentioned in the comments the issue might be that your variable declarations contain whitespace. The following regex matches these (now using the *
operator):
$ echo "double **" | grep "double\s*\*\*"
double **
Answered By - dtell