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.

Gilles 'SO- stop being evil'
865k204 gold badges1.8k silver badges2.3k bronze badges
asked Nov 7, 2013 at 15:43
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.

terdon
252k69 gold badges480 silver badges717 bronze badges
answered Nov 7, 2013 at 15:51
2
  • See also: fgrep (easier to remember than the option version for me). Commented Nov 7, 2013 at 21:24
  • @JoeAtzberger - fgrep & egrep are deprecated and should no longer be used: unix.stackexchange.com/questions/17949/… Commented 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).

answered Nov 7, 2013 at 16:04
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#) Commented Nov 7, 2013 at 17:19
0

how about this?

egrep "\\\$form\['"
answered Nov 7, 2013 at 15:51
1
  • by using ERE you add the requirement to quote the $, so it's a step in the wrong direction. Commented 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']
answered Nov 7, 2013 at 16:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.