站长资讯网
最全最丰富的资讯网站

awk将txt转换为csv

从mysql的表中导出来的txt数据格式:(mysql -e “select * from table_name;”)

  [admin@93a17e41-ee44-4c53-8432-b196632b8891 ~]$ head choushu.log    1 13 56154 49817 ChargeMain_Shortcut 56154 0.33 1302 0.00863122 0.33863122 2018-09-18  2 7 1169 1066 ChargeMain_Shortcut 56154 0.00687561 39 0.01237113 0.01924674 2018-09-18  3 12 5016 4476 ChargeMain_Shortcut 56154 0.02948286 185 0.01371007 0.04319293 2018-09-18  4 10 781 720 ChargeMain_Shortcut 56154 0.00459549 24 0.01144244 0.01603793 2018-09-18  5 9 1331 1198 ChargeMain_Shortcut 56154 0.00782762 40 0.0112844 0.01911202 2018-09-18  6 14 6394 5262 ChargeMain_Shortcut 56154 0.0375808 196 0.01235227 0.04993307 2018-09-18  7 5 4111 3615 ChargeMain_Shortcut 56154 0.02416454 97 0.00894358 0.03310812 2018-09-18  8 6 1336 1107 ChargeMain_Shortcut 56154 0.007857 164 0.0491426 0.0569996 2018-09-18

需要转换为csv格式

第一种方法就是通过excel,导入,然后固定宽度的方式保存

有个缺点就是当表特别大的时候,可能会卡主;而且不方便实现自动化;

我们现在用awk工具实现;

mysql -h address-P port -D database -u username -ppassword -e
“select * from tb_name;” |awk ‘{for(i=1;i<=NF;i++) {if(i<11) printf $i”,”;else print $i}}’ > choushu.csv

结果:

  1,13,56154,49817,ChargeMain_Shortcut,56154,0.33,1302,0.00863122,0.33863122,2018-09-18  2,7,1169,1066,ChargeMain_Shortcut,56154,0.00687561,39,0.01237113,0.01924674,2018-09-18  3,12,5016,4476,ChargeMain_Shortcut,56154,0.02948286,185,0.01371007,0.04319293,2018-09-18  4,10,781,720,ChargeMain_Shortcut,56154,0.00459549,24,0.01144244,0.01603793,2018-09-18  5,9,1331,1198,ChargeMain_Shortcut,56154,0.00782762,40,0.0112844,0.01911202,2018-09-18  6,14,6394,5262,ChargeMain_Shortcut,56154,0.0375808,196,0.01235227,0.04993307,2018-09-18  7,5,4111,3615,ChargeMain_Shortcut,56154,0.02416454,97,0.00894358,0.03310812,2018-09-18

直接双击打开

awk将txt转换为csv

赞(0)
分享到: 更多 (0)
网站地图   沪ICP备18035694号-2    沪公网安备31011702889846号