Skip to main content
edited tags
Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k
Source Link

Help with using quotes in grep searches

I have a file with variables in it of the form a(i)%b(j)%c where the a, b, and c are always the same, but the indices i and j may be different (including multiple characters). So I've played around with grep to find instances of these variables, but success depends upon whether or not I include quotes around my search string, and I'm trying to understand why there are differences. I started with searching for cases of single-character indices:

(1) grep a\(.\)\%b\(.\)\%c file works as expected

(2) grep 'a\(.\)\%b\(.\)\%c' file no matches

(3) grep "a\(.\)\%b\(.\)\%c" file no matches

Then (to confuse myself even further!), I tried including the possibility of multiple-character indices:

(4) grep a\(.*\)\%b\(.*\)\%c file doesn't work - zsh :no matches found: a(.)%b(.)%c

(5) grep 'a\(.*\)\%b\(.*\)\%c' file works

(6) grep "a\(.*\)\%b\(.*\)\%c" file works

Could someone please explain what's happening in each of these cases? In case (4), it looks like the shell (zsh) is doing something different because of the asterisks, but I'm not sure what it's doing. And why (1), (5), and (6) work but (2) and (3) don't is particularly confusing to me.

Thanks!