Unexpected EOF while looking for matching `"'

Hi everyone,

I am trying to search for a string in a file that is partly made up of a variable. Here's the code:

echo "parentCategory = $parentCategory"
echo "parentCategoryFormatted = $parentCategoryFormatted"

numUrlsFoundInParentCategory=`grep -c "<Topic r:id=\"Top\/World\/Fran�ais\/" $inputFile`

echo "Hard-coded result = $numUrlsFoundInParentCategory"

numUrlsFoundInParentCategory=`grep -c \""<Topic r:id=\\\"$parentCategoryFormatted"\" $inputFile`

echo grep -c \""<Topic r:id=\\\"$parentCategoryFormatted"\" $inputFile

echo "Dynamic result = $numUrlsFoundInParentCategory"

It produces the output...

parentCategory = Top/World/Fran�ais/
parentCategoryFormatted = Top\/World\/Fran�ais\/
Hard-coded result = 22093
./dmozStats.sh: command substitution: line 44: unexpected EOF while looking for matching `"'
./dmozStats.sh: command substitution: line 45: syntax error: unexpected end of file
grep -c "<Topic r:id=\"Top\/World\/Fran�ais\/" content.rdf.u8.clean
Dynamic result =

I can't find out why the command using the variable won't work when it appears to look the same as the hard-coded version.

Thanks!

I think that this is all to do with nested quotes and cascading backslashes! :confused:

Try setting the -x option in the script and see if the trace output helps.

Jerry

You seem to have gone a bit too far with the backslashes! :slight_smile:

I'm assuming your input file looks a bit like this:

cat zzz.txt
<Topic r:id="Top/World/Fran�ais/
<Topic r:id="Top/World/Fran�ais/
dfgg
<Topic r:id="Top/World/Fran�ais/

If you can set your variable differently it should work:

parentCategoryFormatted="\"Top/World/Fran�ais/"
numUrlsFoundInParentCategory=`grep -c "<Topic r:id=$parentCategoryFormatted" zzz.txt`
echo "Dynamic result = $numUrlsFoundInParentCategory"
Dynamic result = 3