I need to rename the field names (attributes) in a shapefile. Is there an easy way to do this from the command line?
1 Answer
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).
-
2You can also list the field names using
ogrinfo -so inputfile.shp inputfile
– Mike TNov 12, 2014 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. Sep 4, 2015 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 Dec 28, 2016 at 10:10