136

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?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Avinash Sonee
  • 1,701
  • 2
  • 15
  • 17

6 Answers6

213

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
Zombo
  • 1
  • 62
  • 391
  • 407
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
128

The one that worked for me is:

grep -e '->'

The -e means that the next argument is the pattern, and won't be interpreted as an argument.

From: http://www.linuxquestions.org/questions/programming-9/how-to-grep-for-string-769460/

Zombo
  • 1
  • 62
  • 391
  • 407
The Student
  • 27,520
  • 68
  • 161
  • 264
  • str='! " # $ % & ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~' ; echo "$str" | grep -e "$str" ..... NOT WORKING, what worked for you didn't had enough special characters besides switch sign '-', but win -F instead of -e is WORKING ! – THESorcerer Oct 25 '15 at 08:52
  • 1
    The only thing works for me, even '\' or -F or fgrep do not work. Thanks! – user180574 Aug 13 '18 at 19:17
  • 2
    This should be the accepted answer - simple and works for grep – Ishay Peled Dec 12 '18 at 22:26
  • 3
    Tried -F and fgrep doesn't work for me when searching a string include '-' char. This works. – 世界知名伟人 Jan 25 '19 at 06:53
  • I'm wondering why this answer is present in this question (https://stackoverflow.com/q/12387685 for reference to Q&A I'm currently viewing) - probably a wrong case of answers getting merged from another question? To search for patterns starting with `-`, see also https://stackoverflow.com/questions/2427913/how-can-i-grep-for-a-string-that-begins-with-a-dash-hyphen – Sundeep May 18 '20 at 10:36
  • please mark this one as the accepted answer. The -F option (currently marked as the accepted doesnot work) – OAH May 15 '21 at 07:28
  • This works, not -F, not fgrep. Tested on MacOS Catalina – auspicious99 Oct 23 '21 at 14:59
  • This works for me on a Mac – Jacob Waters Oct 30 '21 at 05:54
  • trying to find all pointers of pointers in the c file with `grep -e "->*->" a.c` doesn't seem to work . the * picks up all sorts of garbage. any suggestions ? – Jay D Jan 29 '23 at 22:00
9

A related note

To grep for carriage return, namely the \r character, or 0x0d, we can do this:

grep -F $'\r' application.log

Alternatively, use printf, or echo, for POSIX compatibility

grep -F "$(printf '\r')" application.log

And we can use hexdump, or less to see the result:

$ printf "a\rb" | grep -F $'\r' | hexdump -c
0000000   a  \r   b  \n

Regarding the use of $'\r' and other supported characters, see Bash Manual > ANSI-C Quoting:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard

ryenus
  • 15,711
  • 5
  • 56
  • 63
3
grep -n "\*\^\%\Q\&\$\&\^\@\$\&\!\^\@\$\&\^\&\^\&\^\&" test.log
1:*^%Q&$&^@$&!^@$&^&^&^&
8:*^%Q&$&^@$&!^@$&^&^&^&
14:*^%Q&$&^@$&!^@$&^&^&^&
Zombo
  • 1
  • 62
  • 391
  • 407
Mani
  • 965
  • 6
  • 10
2

You could try removing any alphanumeric characters and space. And then use -n will give you the line number. Try following:

grep -vn "^[a-zA-Z0-9 ]*$" application.log

Marvin Rabe
  • 4,141
  • 3
  • 25
  • 43
R. Kumar
  • 103
  • 5
-4

Try vi with the -b option, this will show special end of line characters (I typically use it to see windows line endings in a txt file on a unix OS)

But if you want a scripted solution obviously vi wont work so you can try the -f or -e options with grep and pipe the result into sed or awk. From grep man page:

Matcher Selection -E, --extended-regexp Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

   -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.)
  • 2
    In what way this is better than an accepted answer so you added it to almost 5-years old question? – SergGr Mar 02 '17 at 17:13