150

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

226

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
Sign up to request clarification or add additional context in comments.

7 Comments

Depending your version of grep you only need to escape (with a backslash) ^, $, ., * and \ . or you can try fgrep, too.
@NahuelFouilleul: +1. btw, fgrep is same as grep -F.
@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
Doesn't work. grep -rinF '-static' prints Invalid option «t». The @Mani's answer worked, i.e. grep -rin "\-static".
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.
|
133

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 Comments

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 !
The only thing works for me, even '\' or -F or fgrep do not work. Thanks!
This should be the accepted answer - simple and works for grep
Tried -F and fgrep doesn't work for me when searching a string include '-' char. This works.
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/…
|
10

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

Comments

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

Comments

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 Comment

Thanks.. It's works good for me to find the special characters from a file.
-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 Comment

In what way this is better than an accepted answer so you added it to almost 5-years old question?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.