How to grep an asterisk without escaping?

Suppose I have an abc.txt file that contains the string ab*cd . When I grep this ab*cd pattern with quotes, but without asterisk escaping, it does not work:

  > grep ab * c abc.txt 
 > grep "ab * c" abc.txt 
 > grep 'ab * c' abc.txt 

When I use both quotes and run away, it works

  > grep "ab \ * c" abc.txt 
 ab * cd
 > grep 'ab \ * c' abc.txt 
 ab * cd

Now I wonder why the quotes do not work, and if I can only use quotes without escaping the asterisk.

+6
source share
2 answers

Use the -F flag to search for fixed strings - instead of regular expressions. From man grep :

  -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) 

For instance:

 $ grep -F "ab*c" <<< "ab*c" ab*c 
+12
source

First of all, you should keep in mind: regex =/= glob

* has special meaning in regular expression. You must avoid this to fit it literally. without leaving * , grep tries to match ab+(any number of b)+c

eg:

abbbbbbbbbbbbc

+6
source

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


All Articles