1.想把文件中的每列值取出来,然后作为insert 语句中的值,写成insert SQL。
2.同时文件中的列数是不固定的,可能先需要对列数进行一个判定。_
文件样本:
955,2018,10,1, ,FMSCCCM ,2018-10-19 15:04:55.511
602,2018,10,1,602,44638994,2018-10-22 02:49:28.944
608,2018,10,1,608,44638994,2018-10-22 02:49:28.946
610,2018,10,1,610,44638994,2018-10-22 02:49:28.948
612,2018,10,1,612,44638994,2018-10-22 02:49:28.95
613,2018,10,1, ,FMSCCCM ,2018-10-19 15:06:15.403
615,2018,10,1,615,44638994,2018-10-22 02:49:28.952
616,2018,10,1,616,44638994,2018-10-22 02:49:28.953
618,2018,10,1,618,44638994,2018-10-22 02:49:28.955
619,2018,10,1, ,FMSCCCM ,2018-10-19 15:07:19.518
期望结果是生成insert SQL:
insert into tablename values ('955','2018','10','1',' ','FMSCCCM' ,'2018-10-19 15:04:55.511');
insert into tablename values ('602','2018','10','1','602 ','44638994' ,'2018-10-22 02:49:28.944');
insert into tablename values ('608','2018','10','1','608 ','44638994' ,'2018-10-22 02:49:28.946');
insert into tablename values ('610','2018','10','1','610 ','44638994' ,'2018-10-22 02:49:28.948);
目前能写出来把文件每一列值取出,作为insert语句中的值
如:
while read line
do
echo $line > tmp
col1=awk -F "," '{print $1}' tmp
col2=awk -F "," '{print $2}' tmp
col3=awk -F "," '{print $3}' tmp
col4=awk -F "," '{print $4}' tmp
col5=awk -F "," '{print $5}' tmp
col6=awk -F "," '{print $6}' tmp
col7=awk -F "," '{print $7}' tmp
echo "insert into tablename values ('$col1','$col2','$col3','$col4','$col5','$col6','$col7');"
done < test4.log
但是由于文件中的**列数不固定**,脚本目前是写死了7列,不知道列数不一样的话,如何先对列进行一个判定,然后再根据实际列数进行取值!
求指教!