需求说明:整理mongo数据库表信息,现有所有表字段信息与表名一一对应,需要将表名插入对应的字段前
#! /bin/sh
grep -n ^_id mongo.txt | awk -F: '{print $1}' > num.txt
table_count=`cat -n tables.txt | awk '{print $1}' | tail -n 1`
target_row_count=`wc -l num.txt | awk '{print $1}'`
for ((i=1; i<=$target_row_count; i++));
do
for ((j=1; j<=$table_count; j++));
do
row=`sed -n ''"$i"'p' num.txt`
row=`expr $row + $i - 1`
table=`sed -n ''"$j"'p' tables.txt`
if [ $i == $j ]
then
sed -i ''"$row"'i'"$table"'' pinsurvey-mongo.txt
fi
done
done
# 字段信息,_id唯一
# cat mongo.txt
_id ObjectId
created_at Date
geometry.coordinates Array
geometry.type String
properties Object
properties.completion_rate Number
_id ObjectId
photo_id Number
url String
_id ObjectId
geometry Object
geometry.coordinates Array
geometry.type String
# 表名
cat tables.txt
entitys
photos
track_points
承蒙高人指点优化后:
#! /bin/sh
grep -n ^_id mongo.txt | awk -F: '{print $1}' > num.txt
table_count=`cat tables.txt | wc -l`
for ((j=1; j<=$table_count; j++));
do
row=`sed -n ''"$j"'p' num.txt`
row=`expr $row + $j - 1`
table=`sed -n ''"$j"'p' tables.txt`
sed -i ''"$row"'i'"$table"'' mongo.txt
done