Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep with quotation mark

Tags:

grep

bash

I'm trying to scan an error log for lines with 503 errors, so I'm grepping for " 503 (quote space 503).

This seems simple, but it won't work:

grep '" 503 ' access.log

I get the following error:

bash: -c: line 0: unexpected EOF while looking for matching `"' bash: -c: line 1: syntax error: unexpected end of file

like image 326
dtbarne Avatar asked Aug 04 '11 18:08

dtbarne


2 Answers

Seems like you are running it via some system() in some language, aren't you? Try:

grep '\" 503 ' access.log

or:

grep "\" 503 " access.log

Directly in shell just grep '" 503 ' access.log will work. To reproduce your problem I must do:

bash -c 'grep '\" 503 ' access.log'

This is indeed syntax error. To make that work, I need:

bash -c 'grep "\" 503 " access.log'

You are somehow calling bash -c .... Maybe indirectly. You need to figure you how it's called to figure out what quotes are in collision.

like image 197
Michał Šrajer Avatar answered Oct 08 '22 14:10

Michał Šrajer


I believe I have it working now (not sure because I got no results, but didn't get an error).

The reason is because I'm passing it through an ssh command like the following and I believe SSH is doing some escape trickery:

ssh 123.123.123.123 grep '" 503 ' access.log

Modifiying it to this seems to be the fix:

ssh 123.123.123.123 "grep '\" 503 ' access.log"

Thanks for everyone's time.

like image 45
dtbarne Avatar answered Oct 08 '22 15:10

dtbarne