我有一个包含数据的巨大csv文件,我正在尝试生成mysql bulk insert语句。
CSV中显示的数据如下
90927597|1356976813998|1356976814177|1356976817457|17756249959|17756249959|18663111085|17753220005|1090917|1775624995900|A
90927599|1356976813098|1356976814797|1356976823738|12562175250|12562175250|12566502514|12565207040|1890362|1256217525000|A
90927602|1356976813098|1356976814797|1356976823738|12562175250|12562175250|12566502514|12565207040|1890362|1256217525000|A
90927603|1356976813098|1356976814797|1356976823738|12562175250|12562175250|12566502514|12565207040|1890362|1256217525000|A
我希望创建一个大容量插入,它将每两行合并成一个insert语句
具有所需输出的mysql语句
insert into data (90927597,1356976813998,1356976814177,1356976817457,17756249959,17756249959,18663111085,17753220005,1090917,1775624995900,A,90927599,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A);
insert into data (90927602,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A, 90927603,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A);
谢谢你
最佳答案
$ cat tst.awk
BEGIN{ FS="|"; OFS="," }
{
$1 = $1
head = ( (NR%2) == 1 ? tail "insert into data (" : "," )
printf "%s%s", head, $0
tail = ");\n"
}
END { printf "%s", tail }
$ awk -f tst.awk file
insert into data (90927597,1356976813998,1356976814177,1356976817457,17756249959,17756249959,18663111085,17753220005,1090917,1775624995900,A,90927599,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A);
insert into data (90927602,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A,90927603,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A);
关于mysql - awk从csv创建批量MySQL插入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16544747/