Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to search all files in the current directory and below for the substring $form['#node']. So, I have tried, initially:

grep -R "\$form\[\\\\'" . and grep -R "\$form\[\'" .

But it returns no results. However,

grep -R "\$form\[" returns plenty of results, as there are instances of entries such as $form['#id'] or $form['#node'] in the files. Is there any reason why adding the escaped single quote causes an issue? I can't seem to find any clear indication of exactly how to escape a single quote in this case.

I need to check that my escaped characters are working as intended in this scenario.

share|improve this question

4 Answers 4

up vote 6 down vote accepted

I think if you use the swtich -F you'll instruct grep to look for fixed strings.

$ grep -F "\$form['#node']" file.txt

Example

Sample file.

$ cat file.txt
$form['#node']
abc$form['#node']abc
123$form['#node']123
blah blah $form['#node']
someotherstring

Sample run.

$ grep -F "\$form['#node']" file.txt 
$form['#node']
abc$form['#node']abc
123$form['#node']123
blah blah $form['#node']

NOTE: You still need to escape the dollar sign $ because the double quotes wrapping the string are weak and don't guard against the shell, Bash, from thinking that's a variable name.

share|improve this answer
    
Perfect, thankyou. –  njp Nov 7 '13 at 15:59
    
@njp - you're quite welcome, thanks for the Q. –  slm Nov 7 '13 at 16:00
    
See also: fgrep (easier to remember than the option version for me). –  Joe Atzberger Nov 7 '13 at 21:24
    
@JoeAtzberger - fgrep & egrep are deprecated and should no longer be used: unix.stackexchange.com/questions/17949/… –  slm Nov 7 '13 at 21:29

While it's a case where you do want to use -F here, the only character that is special without -F here is [ so is the only one that needs escaped for grep, $ needs to be escaped for the shell, but not for grep as although $ is a regex operator, it is not in that position. [ is a shell globbing operator, but not inside double-quotes

grep -r "\$form\['#node']" .

should work. Of course, as pointed by @slm, there's no reason not to use -F here.

grep -Fr "\$form['#node']" .

(with GNU grep (at least recent versions thereof), -r should be preferred over -R unless you do intend to traverse symlinks).

share|improve this answer
    
+1, for showing both forms. Either using -F (ie, grep -F "string" looks for exactly "string" without special meanings in the characters in "string"), or using "regular" grep (ie, with special regex operators, in which case you need to escape "[" for grep, otherwise grep would consider that you want a string containing $form followed by 1 of the letters amongst ', #, n, o, d or e, for example: $form#) –  Olivier Dulac Nov 7 '13 at 17:19

how about this?

egrep "\\\$form\['"
share|improve this answer
    
by using ERE you add the requirement to quote the $, so it's a step in the wrong direction. –  Stéphane Chazelas Nov 7 '13 at 16:08

Single quotes have no special meaning in regular expressions and since your pattern is already enclosed in double quotes, there is no reason to escape them. Since they are not special characters, escaping them does not work and will screw up your pattern.

Therefore, all you need is "\$form\['":

$ cat file.txt 
$form['#node']
abc$form['#node']abc
123$form['#node']123
blah blah $form['#node']
someotherstring

$ grep  "\$form\['" file.txt 
$form['#node']
abc$form['#node']abc
123$form['#node']123
blah blah $form['#node']
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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