Reverse grep regex matches all lines

$ cat file
anna
amma
kklks
ksklaii

$ grep '\`' file
anna
amma
kklks
ksklaii

Why? How does this match work?

+4
source share
2 answers

twm answer provides a key pointer, but note that this is a sequence\` , and not `by itself, that acts as a start anchor in GNU regular expressions .

Thus, in order to match the literal backward key in the regular expression specified as a single quote string, you do not need escaping at all, either with GNU grepor with BSD / macOS grep:

$ { echo 'ab'; echo 'c`d'; } | grep '`'
c`d

, , , - , ` , grep:

$ { echo 'ab'; echo 'c`d'; } | grep "\`"
c`d

, , "...", grep `.

GNU grep:

$ { echo 'ab'; echo 'c`d'; } | grep "\\\`" # !! BOTH \ and ` need \-escaping
ab
c`d

, grep \`, GNU grep , .

  • , grep , \` , ^ ; , , , grep -z - \` .

BSD/macOS grep, \` `, , .

+2

, -, GNU . backtick ('\ `') , , . OS X, -, GNU, , . . http://www.regular-expressions.info/gnu.html

GNU-, :

grep '[`]' file
+6

Source: https://habr.com/ru/post/1612852/


All Articles