使用bash脚本将文件夹中的所有dbf追加到第一个dbf

使用bash脚本将文件夹中的所有dbf追加到第一个dbf,bash,gis,shapefile,dbf,ogr,Bash,Gis,Shapefile,Dbf,Ogr,我尝试将文件夹中的所有dbf附加到第一个dbf。DBF是ESRI形状文件的一部分,我想将其附加到一个文件中。我有一个工作代码,但我想我所做的真的很糟糕(我是一个绝对的bash新手)。。。当我处理第一个文件时,我的计数器正在循环结束时计算一个多余的文件,并产生一个错误。。 附加由ogr2ogr(GDAL/OGR库)完成 版本A:显式指定dbf要附加的内容 append_to="fff.dbf" find . -maxdepth 1 -name \*.dbf -print0 | grep -zv "

我尝试将文件夹中的所有dbf附加到第一个dbf。DBF是ESRI形状文件的一部分,我想将其附加到一个文件中。我有一个工作代码,但我想我所做的真的很糟糕(我是一个绝对的bash新手)。。。当我处理第一个文件时,我的计数器正在循环结束时计算一个多余的文件,并产生一个错误。。 附加由ogr2ogr(GDAL/OGR库)完成


版本A:显式指定dbf要附加的内容

append_to="fff.dbf"
find . -maxdepth 1 -name \*.dbf -print0 | grep -zv "^$append_to$" | xargs -0 -n1 -I % echo ogr2ogr -append "$append_to" "%"
变量B:附加到第一个dbf(第一个由ls附加)

现在两者都处于“干运行”模式-仅显示将执行的操作。满足要求时,从xargs中移除
echo
。两个版本的第二行相同

纯粹的狂欢

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
记录在案 如果使用ogr2ogr附加形状文件的DBF,事实上事情会简单得多。如果传递不存在alread的shp文件名,它会动态创建一个空形状文件并将数据附加到该文件中。因此,这就足够了:

# working directory with shp-files to be appended into one file
mydir=D:/GIS_DataBase/CorineLC/shps_extracted
cd $mydir

# directory where final shp-file will be saved
mydir_final=D:/GIS_DataBase/CorineLC/shps_app_and_extr
mkdir $mydir_final

# get dbfs, which are the actual files to which append the data to
declare -a dbfs=(*.dbf)

# loop through dbfs in dir and append all to the dbf of shp-file
# extr_and_app.shp that will be created by ogr2ogr on the fly 
# and saved to {mydir_final}
for dbf in ${dbfs[@]}; do
  echo appending $dbf to $mydir_final/extr_and_app.dbf
  ogr2ogr -append $mydir_final/extr_and_app.dbf $dbf
done

我使用最后一种方法-谢谢和+1。但是,对于任何使用ogr2ogr的人,请查看我发布的附加答案,以备记录!你为自己找到了最好的解决方案。我第一次在这里听说了
ogr2ogr
。。。所以,接受的thanx:)
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
# working directory with shp-files to be appended into one file
mydir=D:/GIS_DataBase/CorineLC/shps_extracted
cd $mydir

# directory where final shp-file will be saved
mydir_final=D:/GIS_DataBase/CorineLC/shps_app_and_extr
mkdir $mydir_final

# get dbfs, which are the actual files to which append the data to
declare -a dbfs=(*.dbf)

# loop through dbfs in dir and append all to the dbf of shp-file
# extr_and_app.shp that will be created by ogr2ogr on the fly 
# and saved to {mydir_final}
for dbf in ${dbfs[@]}; do
  echo appending $dbf to $mydir_final/extr_and_app.dbf
  ogr2ogr -append $mydir_final/extr_and_app.dbf $dbf
done