批量---多行文件按需插入指定各行

需求说明:整理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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值