如何用括号和引号grep一个字符串?

时间:2022-09-15 16:05:43

I'm trying to grep the following in my directory:

我正在尝试在我的目录中grep以下内容:

$form['#submit'][1]

I've tried various permutations possible of escaping characters, including:

我尝试了各种可能的转义字符排列,包括:

grep -r "$form\['#submit'\]\[1\]" ./*
grep -r "$form\['#submit']\[1]" ./*
grep -r "$form\['#submit']\[1]" *
grep -r "$form\['#submit'][1]" *
grep -r "$form\[\'#submit'][1]" *
grep -r "$form\[\'#submit']\[1]" *
grep -r "$form\[\'#submit\']\[1]" *
grep -r "$form\[\'#submit\'\]\[1\]" *
grep -r "\$form\[\'#submit\'\]\[1\]" *
grep -r "$form\[\'#submit\'\]\[1\]" *
grep -r "$form['#submit'][1]" ./*

But nothing seems to be getting the correct terms. How do I grep this expression?

但似乎没有任何东西可以得到正确的条款。我如何grep这个表达式?

3 个解决方案

#1


About escaping

Basically you just need to escape the characters which have a meaning in basix posix regexes, which are:

基本上你只需要逃避在basix posix regex中有意义的字符,它们是:

  • $ End of line
  • $行结束

  • [ Start of character class
  • [角色类的开始

  • ] End of character class
  • ]字符类结束

Additionally you need to use double quotes to enclose the pattern since it contains single quotes itself. But in this case you need to double escape the $ since it has a meaning when inside shell's double quotes (start of variable) and the regex pattern (end of line).

此外,您需要使用双引号来包含模式,因为它本身包含单引号。但是在这种情况下你需要双重转义$,因为它在shell的双引号(变量的开始)和正则表达式模式(行尾)中有意义。

grep -r "\\$form\['#submit'\]\[1\]"

About fixed strings instead of patterns

关于固定字符串而不是模式

On the other hand you need to know that grep is also able to search for fixed strings instead of regex patterns. You need to pass -F in that case:

另一方面,您需要知道grep还能够搜索固定字符串而不是正则表达式模式。在这种情况下你需要传递-F:

grep -rF "\$form['#submit'][1]"

Now, only the $ needs to be escaped since bash would otherwise try to expand the subsequent characters as a variable name.

现在,只有$需要被转义,因为bash否则会尝试将后续字符扩展为变量名。

#2


I would suggest to avoid excessive escaping use -F option for fixed string search:

我建议避免过度转义使用-F选项进行固定字符串搜索:

grep -riF "\$form['#submit'][1]" .

$ still needs to be escape since whole string is in double quotes.

由于整个字符串都是双引号,因此仍需要转义$。

#3


You need to quote $ [ and ]

你需要引用$ [和]

Test

$ cat input
$form['#submit'][1]

$ grep "\$form\['#submit'\]\[1\]" input
$form['#submit'][1]

Why ?

  • $ To escape the special meaning of $ in bash/shell and prevent variable expansion

    $在bash / shell中逃避$的特殊含义并防止变量扩展

  • [ and ] Escape the special meaning of [ of character class

    [和]逃避[字符类的特殊含义

Shorter method

$ grep "\$form\['#submit']\[1]" input
$form['#submit'][1]

Here only the opening [ is escaped which causes any occurence of lone ] to be treated as normal characters without special meaning

这里只有开口[被转义导致任何出现的孤独]被视为普通字符而没有特殊含义

#1


About escaping

Basically you just need to escape the characters which have a meaning in basix posix regexes, which are:

基本上你只需要逃避在basix posix regex中有意义的字符,它们是:

  • $ End of line
  • $行结束

  • [ Start of character class
  • [角色类的开始

  • ] End of character class
  • ]字符类结束

Additionally you need to use double quotes to enclose the pattern since it contains single quotes itself. But in this case you need to double escape the $ since it has a meaning when inside shell's double quotes (start of variable) and the regex pattern (end of line).

此外,您需要使用双引号来包含模式,因为它本身包含单引号。但是在这种情况下你需要双重转义$,因为它在shell的双引号(变量的开始)和正则表达式模式(行尾)中有意义。

grep -r "\\$form\['#submit'\]\[1\]"

About fixed strings instead of patterns

关于固定字符串而不是模式

On the other hand you need to know that grep is also able to search for fixed strings instead of regex patterns. You need to pass -F in that case:

另一方面,您需要知道grep还能够搜索固定字符串而不是正则表达式模式。在这种情况下你需要传递-F:

grep -rF "\$form['#submit'][1]"

Now, only the $ needs to be escaped since bash would otherwise try to expand the subsequent characters as a variable name.

现在,只有$需要被转义,因为bash否则会尝试将后续字符扩展为变量名。

#2


I would suggest to avoid excessive escaping use -F option for fixed string search:

我建议避免过度转义使用-F选项进行固定字符串搜索:

grep -riF "\$form['#submit'][1]" .

$ still needs to be escape since whole string is in double quotes.

由于整个字符串都是双引号,因此仍需要转义$。

#3


You need to quote $ [ and ]

你需要引用$ [和]

Test

$ cat input
$form['#submit'][1]

$ grep "\$form\['#submit'\]\[1\]" input
$form['#submit'][1]

Why ?

  • $ To escape the special meaning of $ in bash/shell and prevent variable expansion

    $在bash / shell中逃避$的特殊含义并防止变量扩展

  • [ and ] Escape the special meaning of [ of character class

    [和]逃避[字符类的特殊含义

Shorter method

$ grep "\$form\['#submit']\[1]" input
$form['#submit'][1]

Here only the opening [ is escaped which causes any occurence of lone ] to be treated as normal characters without special meaning

这里只有开口[被转义导致任何出现的孤独]被视为普通字符而没有特殊含义