146

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?

1
  • ^@ : means ^ followed by @ or nul character? Commented Sep 12, 2012 at 12:19

6 Answers 6

221

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
7
  • 10
    Depending your version of grep you only need to escape (with a backslash) ^, $, ., * and \ . or you can try fgrep, too. Commented Sep 13, 2012 at 7:33
  • 3
    @NahuelFouilleul: +1. btw, fgrep is same as grep -F. Commented Sep 13, 2012 at 8:18
  • 1
    @PrinceJohnWesley: it's the same but on some systems there are many versions of grep so using fgrep may find the version which support -F Commented Sep 13, 2012 at 9:34
  • 1
    Doesn't work. grep -rinF '-static' prints Invalid option «t». The @Mani's answer worked, i.e. grep -rin "\-static".
    – Hi-Angel
    Commented Aug 31, 2014 at 18:39
  • 1
    Did not work on Ubuntu 20.04.5 with grep -Fn '#!/usr/bin/python' --include=*.py, no any output shown and seems stucked in console.
    – Keelung
    Commented Mar 21, 2023 at 2:35
132

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/

9
  • 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 ! Commented Oct 25, 2015 at 8:52
  • 1
    The only thing works for me, even '\' or -F or fgrep do not work. Thanks!
    – user180574
    Commented Aug 13, 2018 at 19:17
  • 2
    This should be the accepted answer - simple and works for grep Commented Dec 12, 2018 at 22:26
  • 3
    Tried -F and fgrep doesn't work for me when searching a string include '-' char. This works. Commented Jan 25, 2019 at 6:53
  • I'm wondering why this answer is present in this question (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 stackoverflow.com/questions/2427913/…
    – Sundeep
    Commented May 18, 2020 at 10:36
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

3
grep -n "\*\^\%\Q\&\$\&\^\@\$\&\!\^\@\$\&\^\&\^\&\^\&" test.log
1:*^%Q&$&^@$&!^@$&^&^&^&
8:*^%Q&$&^@$&!^@$&^&^&^&
14:*^%Q&$&^@$&!^@$&^&^&^&
0
3

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

1
  • Thanks.. It's works good for me to find the special characters from a file. Commented Jun 28, 2018 at 4:36
-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.)
1
  • 2
    In what way this is better than an accepted answer so you added it to almost 5-years old question?
    – SergGr
    Commented Mar 2, 2017 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.