I try to append all dbfs in a folder to the first dbf. The dbfs are part of ESRI shapefiles that I want to append into one file. I got a working code but I guess what I did is really awkard (I'm an absolute bash newbie)... And as I ommit the first file my counter is counting one excessive file in the end of the loop and producing an error.. The appending is done by ogr2ogr (GDAL/OGR Library)
mydir=C:/Users/Kay/Desktop/Test_GIS/new/
cd $mydir
dbfs=(*.dbf) # put dir to array
let i=1 # start with 1 omitting 1st file with index 0
for f in *.dbf
do
echo appending file ${dbfs[i]} to ${dbfs[0]}
ogr2ogr -append ${dbfs[0]} ${dbfs[i]}
let i=i+1 # counter + 1
done
Version A: you explicitly specify to what dbf want append
append_to="fff.dbf"
find . -maxdepth 1 -name \*.dbf -print0 | grep -zv "^$append_to$" | xargs -0 -n1 -I % echo ogr2ogr -append "$append_to" "%"
Variant B: appending to the 1st dbf (1st by ls)
append_to=$(ls -1 *.dbf | head -1)
find . -maxdepth 1 -name \*.dbf -print0 | grep -zv "^$append_to$" | xargs -0 -n1 -I % echo ogr2ogr -append "$append_to" "%"
Both are now in the "dry run" mode - only shows what will do. When satisfied remove the echo
from the xargs. The second line is same for both versions.
pure bash
IFS=$'\t\n' #don't need this line when your filenames doesn't contain spaces
declare -a dbfs=(*.dbf)
unset $IFS #don't need this line when your filenames doesn't contain spaces
append_to=${dbfs[0]}
unset dbfs[0]
for dbf in ${dbfs[@]}
do
echo ogr2ogr -append "$append_to" "$dbf"
done