$ cat file
anna
amma
kklks
ksklaii
$ grep '\`' file
anna
amma
kklks
ksklaii
为什么?那场比赛如何运作?
$ cat file
anna
amma
kklks
ksklaii
$ grep '\`' file
anna
amma
kklks
ksklaii
为什么?那场比赛如何运作?
这似乎是正则表达式的 GNU 扩展。反引号 ('\`') 锚匹配主题字符串的开头,这解释了为什么它匹配所有行。OS X 显然没有实现 GNU 扩展,这可以解释为什么您的示例与那里的任何行都不匹配。见http://www.regular-expressions.info/gnu.html
如果您想在 GNU 扩展生效时匹配实际的反引号,这对我有用:
grep '[`]' file
twm's answer provides the crucial pointer, but note that it is the sequence \`
, not `
by itself that acts as the start-of-input anchor in GNU regexes.
Thus, to match a literal backtick in a regex specified as a single-quoted shell string, you don't need any escaping at all, neither with GNU grep
nor with BSD/macOS grep
:
$ { echo 'ab'; echo 'c`d'; } | grep '`'
c`d
When using double-quoted shell strings - which you should avoid for regexes, for reasons that will become obvious - things get more complicated, because you then must escape the `
for the shell's sake in order to pass it through as a literal to grep
:
$ { echo 'ab'; echo 'c`d'; } | grep "\`"
c`d
Note that, after the shell has parsed the "..."
string, grep
still only sees `
.
To recreate the original command with a double-quoted string with GNU grep
:
$ { echo 'ab'; echo 'c`d'; } | grep "\\\`" # !! BOTH \ and ` need \-escaping
ab
c`d
Again, after the shell's string parsing, grep
sees just \`
, which to GNU grep
is the start-of-the-input anchor, so all input lines match.
grep
processes input line by line, \`
has the same effect as ^
the start-of-a-line anchor; with multi-line input, however - such as if you used grep -z
to read all lines at once - \`
only matches the very start of the whole string.To BSD/macOS grep
, \`
simply escapes a literal `
, so it only matches input lines that contain that character.