[GRASS-user] Rename multiple sqlite columns at once

Moritz Lennert mlennert at club.worldonline.be
Wed Nov 5 11:33:29 EST 2008


On 05/11/08 15:17, Nikos Alexandris wrote:
> On Tue, 2008-11-04 at 10:35 +0100, Paolo Craveri wrote:
>> Hello Nikos
>>
>>> The command
>>> v.db.renamecol map=test_rencol column="AREA","area"
>>>
>>> does not work. I assume that trying to use 'tr' for example wouldn't
>>> help (?). Or am I doing something wrong?
>>>
>>
>> Try this:
>>
>>
>> ==============
>> #!/bin/sh
>> # # # in this example I use nc_spm_07 dataset
>> # # # I created a sqlite mapset
>> db.connect driver=sqlite \
>> 	database='$GISDBASE/$LOCATION_NAME/$MAPSET/my_sqlitedb.db'
>> db.connect -p
>> g.copy vect=hospitals,myhospitals --overwrite
>> v.db.connect myhospitals -p
>> db.columns myhospitals
>> #
>> # use awk, function: tolower
>> # echo "MINNIE" | awk '{print tolower($1)}'
>> # minnie
>> # ...and function  substr
>> #  echo "minnie_tmp"| awk '{print substr($1,1,length($1)-length("_tmp"))}'
>> # minnie
>>
>> #skip out duplicate column name error
>> # (no differences between lower and upper case)
>> #
>> #
>> key_field="cat"
>> suffix="_tmp"
>> #THIS MAY TAKE A WHILE...
>> for field in `db.columns myhospitals`; do
>> 	if [ "$field" != "$key_field" ]; then
>> 		tmpfield=`echo $field | awk '{print tolower($1)}'`
>> 		tmpfield=$tmpfield$suffix
>> 		v.db.renamecol map=myhospitals layer=1 column=$field,$tmpfield
>> 		lower_field=`echo $tmpfield $suffix| awk '{print
>> substr($1,1,length($1)-length($2))}'`
>> 		v.db.renamecol map=myhospitals layer=1 column=$tmpfield,$lower_field
>> 	fi
>> done
>>
>> db.columns myhospitals
>> =============================
>>
>>
>>
>> there are same better and more elegant solutions probably...
>>
>>
>> ciao
>>
>>
>> Paolo
> 
> Ciao Paolo!
> 
> Looks like a nice learning-example to me :-)
> 
> It works!! But still I get some error/warning from "v.db.renamecol".
> Don't know, I changed it a bit... :
> ----
> #!/bin/sh
> 
> key_column="cat"
> suffix="_tmp"
> for column in `db.columns test`; do
>         if [ "$column" != "$key_column" ]; then
>                 tempcolumn=`echo $column | awk '{print tolower($1)}'`
>                 tempcolumn=$tempcolumn$suffix
>                 v.db.renamecol map=test layer=1 column=$column,
> $tempcolumn
>                 lower_column=`echo $tempcolumn $suffix| awk '{print
> substr($1,1,length($1)-length($2))}'`
>                 v.db.renamecol map=test layer=1 column=$tempcolumn,
> $lower_column
>         fi
> done
> db.columns test
> ----
> 
> The error is:
> Sorry <_tmp> is not a valid option

I think you might have to escape _tmp with a backslash, i.e. "\_tmp".

Moritz


More information about the grass-user mailing list