Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matching a line with a literal asterisk "*" in grep

Tags:

regex

grep

bash

awk

Tried

$ echo "$STRING" | egrep "(\*)" 

and also

$ echo "$STRING" | egrep '(\*)' 

and countless other variations. I just want to match a line that contains a literal asterisk anywhere in the line.

like image 515
Derrick Avatar asked Oct 17 '09 03:10

Derrick


People also ask

How to use asterisk with grep?

When an asterisk ( * ) follows a character, grep interprets it as "zero or more instances of that character." When the asterisk follows a regular expression, grep interprets it as "zero or more instances of characters matching the pattern." You may want to try this to see what happens otherwise.

How do you escape asterisk in grep?

If you include special characters in patterns typed on the command line, escape them by enclosing them in single quotation marks to prevent inadvertent misinterpretation by the shell or command interpreter. To match a character that is special to grep –E, put a backslash ( \ ) in front of the character.

How do you match an asterisk in regex?

The [a-z]+ matches one or more lowercase letters. The [*]? matches zero or one asterisks. The $ matches the end of the string.


1 Answers

Try a character class instead

echo "$STRING" | egrep '[*]'  
like image 94
pavium Avatar answered Sep 19 '22 04:09

pavium