dtbarne
dtbarne

Reputation: 8200

Grep with quotation mark

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

Upvotes: 15

Views: 30896

Answers (4)

dtbarne
dtbarne

Reputation: 8200

The issue was due to some erroneous directives in .bashrc.

Upvotes: 0

Michał Šrajer
Michał Šrajer

Reputation: 31182

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.

Upvotes: 18

dtbarne
dtbarne

Reputation: 8200

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.

Upvotes: 1

pyroscope
pyroscope

Reputation: 4158

To debug strange effects like this, use "set -x" to show the shell expansions, and what the computer thinks about your command.

Upvotes: 1

Related Questions