I'm using the following code:

while read line; do
for att in $line; do
ogr2ogr -where "newcode='$att'" -f "ESRI Shapefile" $OUTPUT/$att.shp $INDATA
done < $SCRIPT/newcodes.txt

The text file is being read correctly as shapefiles named after all names in the textfile list are being created. However, the shapefiles are empty (no attribute table or polygons). The ogr2ogr line of code works outside of the loop on a single feature within the shapefile. The newcodes.txt contains 3-5 letter codes e.g. aaa, gbe.

I think the problem is getting bash to treat $att as text to search for in the attribute table of the shapefile within the -where switch.

Any help much appreciated.


Attribute Table Sample

The shapefile has 15 columns in the table, and 6506 rows/features.

The 'newcode' field which I'am trying to read with the -where switch was joined to the table. I have tried using the loop with a pre-existing field and it still produced empty shapefiles.

Update 2:

After a bit more interrogation, I've noticed that the shapefiles are being produced with a space after their name. E.g. 'aaa .shp' rather than 'aaa.shp'. This probably means that the $att variable is being read with a space also, and therefore not matching anything in the attribute table. I have checked the text file, and there are no spaces in there. Still unsure how to fix.
enter image description here

e.g. cdg cdh cdi cej cea eig fjj fja fjf

  • see gis.stackexchange.com/questions/24139/… for a similar question and solution
    – Ian Turton
    Oct 9, 2012 at 13:53
  • Can you post the text file?
    – R.K.
    Oct 9, 2012 at 14:14
  • @iant modifying the loop as you've suggested in the link to something like this wclause=newcode="$att" ogr2ogr -where '$wclause' -f "ESRI Shapefile" $OUTPUT/$att.shp $INDATA created a syntax error in the SQL statement.
    – JPD
    Oct 9, 2012 at 15:13
  • You might want to put echo statements so we could see what the actual values of att are.
    – R.K.
    Oct 9, 2012 at 16:14

2 Answers 2


You can try to escaping the dollar sign.

ogr2ogr -where "newcode='\$att'" -f "ESRI Shapefile" $OUTPUT/$att.shp $INDATA

If that doesn't work you can try this

ogr2ogr -where 'newcode="$att"' -f "ESRI Shapefile" $OUTPUT/$att.shp $INDATA
  • Hi - thanks for your reply. Even with the escape, the shapefiles are coming out empty, so the -where switch isn't working.
    – JPD
    Oct 9, 2012 at 11:33
  • Thanks for the second suggestion. Unfortunately, this produces the same empty shapefiles as a result too.
    – JPD
    Oct 9, 2012 at 11:43
  • Can you show the structure of your shapefile's attribute table?
    – R.K.
    Oct 9, 2012 at 11:47
  • Have added some more to the question.
    – JPD
    Oct 9, 2012 at 12:32

It turns out that my text file input for the script had a CTRL+M after each line. This is because I run Linux in a Virtual Machine and originally created the text file with codes in the Windows environment and didn't reformat.

The way I fixed this was to open the .txt file with a text editor and change the format to UNIX as opposed to Windows.

Thank you everyone for your help and sorry it was something as silly as this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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