4

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.

0

4 Answers 4

6

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.

3
  • @njp - you're quite welcome, thanks for the Q.
    – slm
    Nov 7, 2013 at 16:00
  • See also: fgrep (easier to remember than the option version for me). Nov 7, 2013 at 21:24
  • @JoeAtzberger - fgrep & egrep are deprecated and should no longer be used: unix.stackexchange.com/questions/17949/…
    – slm
    Nov 7, 2013 at 21:29
2

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).

1
  • +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#) Nov 7, 2013 at 17:19
0

how about this?

egrep "\\\$form\['"
1
  • by using ERE you add the requirement to quote the $, so it's a step in the wrong direction. Nov 7, 2013 at 16:08
0

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']

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .