20

I need to rename the field names (attributes) in a shapefile. Is there an easy way to do this from the command line?

29

You can use the ogr2ogr utility which is packaged with the gdal command line tools. Use the -sql option as follows:

ogr2ogr outputfile.shp inputfile.shp -sql "SELECT oldfield1 AS newfield1, oldfield2 AS newfield2 from inputfile"

As an added bonus, you can convert the data into a different format at the same time, or filter your data by specifying a where clause. Note that you will need to specify all output fields if you use this method (any fields not included in the SELECT statement will be dropped from the output file).

  • 2
    You can also list the field names using ogrinfo -so inputfile.shp inputfile – Mike T Nov 12 '14 at 2:47
  • A side note: this method only works when transforming shapefiles. For example if using GeoJSON data format you need to first convert it to a shapefile, then do another transformation using the -sql flag. – clhenrick Sep 4 '15 at 13:22
  • Also you should add -dialect SQLITE option otherwise ogr2ogr will throw an error 6: output_file.shp must contain at least one attribute 1 field – Dmitriy Litvinov Dec 28 '16 at 10:10

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.