5

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?

  • 1
    is space after last double quote a typos ? – Archemar Sep 11 '18 at 7:43
  • No, but I also tried with no space – Cyril Sep 11 '18 at 7:47
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.)
  • 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. – Marc van Leeuwen Sep 11 '18 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 Sep 11 '18 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\]"'
  • Thank you for this. This works also but on the real logs file, doesn't work. Good explanation though. – Cyril Sep 11 '18 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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