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.
4 Answers 4
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.
-
See also: fgrep (easier to remember than the option version for me).Joe Atzberger– Joe Atzberger2013年11月07日 21:24:01 +00:00Commented Nov 7, 2013 at 21:24
-
@JoeAtzberger - fgrep & egrep are deprecated and should no longer be used: unix.stackexchange.com/questions/17949/…2013年11月07日 21:29:52 +00:00Commented Nov 7, 2013 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).
-
+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
ore
, for example:$form#
)Olivier Dulac– Olivier Dulac2013年11月07日 17:19:39 +00:00Commented Nov 7, 2013 at 17:19
how about this?
egrep "\\\$form\['"
-
by using ERE you add the requirement to quote the
$
, so it's a step in the wrong direction.Stéphane Chazelas– Stéphane Chazelas2013年11月07日 16:08:07 +00:00Commented Nov 7, 2013 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']