4

How do I grep this? (Including the special characters)

"Limit reached."[\n]"

I tried back-slashing the special symbols but end up not working, like this:

grep '\"Limit reached\.\"\[\\n\]\" '

I also tried other techniques but also not working. Is there any other syntax you could suggest/advice?

2
  • 1
    is space after last double quote a typos ?
    – Archemar
    Commented Sep 11, 2018 at 7:43
  • No, but I also tried with no space
    – Cyril
    Commented Sep 11, 2018 at 7:47

2 Answers 2

11

use -F in grep

$ cat test.txt
"Limit reached."[\n]"
test
"Limit reached."[\n]"

$ grep -F '"Limit reached."[\n]"' test.txt
"Limit reached."[\n]"
"Limit reached."[\n]"

As per Manual page,

   -F, --fixed-strings, --fixed-regexp
          Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.  (-F is specified by> POSIX,
 --fixed-regexp is an obsoleted alias, please do not use it new scripts.)
2
  • There is also the fgrep command, which as per the manpage does just grep -F. Personally I always use fgrep unless I know I am really using a regexp. Commented Sep 11, 2018 at 9:47
  • @MarcvanLeeuwen it might be a good idea to get used to grep -F since fgrep and egrep are only included for backwards compatibility now that POSIX specifies -F and -E.
    – terdon
    Commented Sep 11, 2018 at 12:44
5

You were very close.

You do not need to excape ", and cannot use shell-escape in single quotes. Therefore all escaping is for grep, not for the shell. (Note on single quotes: single quotes does no interpretation. If you need to put a single quote withing a single quoted string, then you have to come out of single quotes e.g. 'don'\''t' )

Test

printf "%s" '"Limit reached."[\n]"' | grep  '"Limit reached\."\[\\n\]"'
1
  • Thank you for this. This works also but on the real logs file, doesn't work. Good explanation though.
    – Cyril
    Commented Sep 11, 2018 at 8:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .