Sharp House

  • Increase font size
  • Default font size
  • Decrease font size

How to: special characters with grep

E-mail

I had this problem with grep where I needed to find characters such as ' or [ and it wouldn't find them, no matter if I had backslashes or not before, and no matter if I used ' to enclose the search string or not - which is what I've always seen used in all the documentation I could find.

So, what's the trick here? Just use " " to enclose your search string, and backslashes before every regular expression special character, because grep assumes that the search string is going to be a regexp.

Example: let's assume that we have a bunch of php files under different subdirectories in /www/ and we want to find all the ones containing a certain variable, let's say $_SERVER['SERVER_NAME']

The correct grep command to find it would be:

grep -r "\$_SERVER\['SERVER_NAME']" /www/*

If you want to learn more about regexp, http://www.regular-expressions.info/ is a great place to start.

Hope this helped, for any question - head to the forums.

 

Credits for this go to: Inc0 and Prose.

 


« May 2013 »
Mo Tu We Th Fr Sa Su
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31