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

Viewed 40769

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.

6 Answers

Simply escape the asterisk with a backslash:

grep "\*"

If there is a need to detect an asterisk in awk, you can either use

awk '/\*/' file

Here, * is used in a regex, and thus, must be escaped since an unescaped * is a quantifier that means "zero or more occurrences". Once it is escaped, it no longer has any special meaning.

Alternatively, if you do not need to check for anything else, it makes sense to peform a fixed string check:

awk 'index($0, "*")' file

If * is found anywhere inside a "record" (i.e. a line) the current line will get printed.

Related