grep for special characters in Unix

Avinash Sonee picture Avinash Sonee · Sep 12, 2012 · Viewed 314.5k times · Source

I have a log file (application.log) which might contain the following string of normal & special characters on multiple lines:

*^%Q&$*&^@$&*!^@$*&^&^*&^&

I want to search for the line number(s) which contains this special character string.

grep '*^%Q&$*&^@$&*!^@$*&^&^*&^&' application.log

The above command doesn't return any results.

What would be the correct syntax to get the line numbers?

Answer

Prince John Wesley picture Prince John Wesley · Sep 12, 2012

Tell grep to treat your input as fixed string using -F option.

grep -F '*^%Q&$*&^@$&*!^@$*&^&^*&^&' application.log

Option -n is required to get the line number,

grep -Fn '*^%Q&$*&^@$&*!^@$*&^&^*&^&' application.log