Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep regex with backtick matches all lines

$ cat file
anna
amma
kklks
ksklaii

$ grep '\`' file
anna
amma
kklks
ksklaii

Why? How is that match working ?

like image 640
Ankur Agarwal Avatar asked Oct 23 '15 00:10

Ankur Agarwal


People also ask

Can you use regex with grep?

The grep command (short for Global Regular Expressions Print) is a powerful text processing tool for searching through files and directories. When grep is combined with regex (regular expressions), advanced searching and output filtering become simple.

How do you grep a tick?

If you don't actually need to match the backtick itself, but you are looking to match something enclosed by a backtick ( like auto-generated MySQL table names, for instance) a single-character wildcard (".") also does the trick.

How do you grep for lines that don't match?

To display only the lines that do not match a search pattern, use the -v ( or --invert-match ) option. The -w option tells grep to return only those lines where the specified string is a whole word (enclosed by non-word characters). By default, grep is case-sensitive.

How do you make grep negative?

To use negative matching in grep , you should execute the command with the -v or --invert-match flags. This will print only the lines that don't match the pattern given.


1 Answers

This appears to be a GNU extension for regular expressions. The backtick ('\`') anchor matches the very start of a subject string, which explains why it is matching all lines. OS X apparently doesn't implement the GNU extensions, which would explain why your example doesn't match any lines there. See http://www.regular-expressions.info/gnu.html

If you want to match an actual backtick when the GNU extensions are in effect, this works for me:

grep '[`]' file
like image 167
twm Avatar answered Nov 13 '22 09:11

twm