我必须解析一个csv文件并将其内容转储到mysql表中。使用awk将csv文件解析为数组
# myfile.csv # Contents # Sample Headers "header1 with quotes", header2withoutquotes, "header3", header4, hdeader5 "Sample Text",2,3,4,"MoreText, with commas" "Text2 with escaped \"",8,6,7,9 "Text3",876,0.6,7,10
首页输出
rowid|header1 with quotes|Sample Text|myfile 1|header2withoutquotes|2|myfile 1|header3|3|myfile 1|header4|4|myfile 1|header5|MoreText, with commas|myfile 2|header1 with quotes|Text2 with escaped \"|myfile 2|header2withoutquotes|8|myfile 2|header3|6|myfile 2|header4|7|myfile 2|header5|9|myfile 3|header1 with quotes|text3|myfile 3|header2withoutquotes|876|myfile 3|header3|0.6|myfile 3|header4|7|myfile 3|header5|10|myfile
在输出的第二我需要自定义页眉进行水平对齐。例如
rowid|"header1 with quotes"|"header3"|header4|filename 1|Sample Text|3,4,myfile 2|Text2 with escaped \"|6|7|myfile 3|Text3|0.6|7|myfile
对于第二个输出,它可以是我选择的任何一组标题。 然后,我可以使用加载数据infile将此输出数据加载到mysql表中。寻找awk脚本来实现这一点。需要帮助请叫我。 TX。
请原谅未格式化的数据。仍在学习...... – user676500
@edmorton按照要求编辑了问题。 – user676500
如果您需要使用嵌入逗号和引号来处理CSV的完整复杂性,那么您可能最好使用Python或Perl以及可用的CSV模块,或者使用专门的工具,如CSVfix(该工具位于Google Code上有一次,但现在已经闭店了;我不确定这些日子的官方消息来源,这很令人尴尬)。 –