需要在mysql中插入单行CSV文件(VoIP速率)。 文件模式:
citycode01,city name01,rate01 citycode02,city name02,rate02 citycode0n,city name0n,rate0n
其中FS =“,”和RS =“”。
如何在单独的行中将单行拆分为每个目标,或者使用AWK构建直接的sql插入文件?
CSV文件
1201,New Jersey,0.012500 1202,USA OUT,0.012500 1203,USA OUT,0.012500 1204,Manitoba,0.008000
需要修改为每行一个目标记录
1201,New Jersey,0.012500
1202,USA OUT,0.012500
1203,USA OUT,0.012500
1204,Manitoba,0.008000
我在awk -F ',' '{print $1$2}' rates.csv
1201 New Jersey
我可以按awk -F '[, ]' '{print $4}' rates.csv
0.012500
但是我不知道如何在整个线路上很好地循环它。
谢谢
答案 0 :(得分:2)
鉴于您的新输入:
1604999,Vancouver - BC,0.008000 1605,USA OUT,0.012500 1605397,USA,0.039000 1605475,,0.061000 1605477,,0.061000 1605692,,0.061000 1605715,,0.061000 1606,USA OUT,0.012500 1607,USA OUT,0.012500 1608,USA OUT,0.012500
使用FPAT
的GNU awk:
$ awk -v FPAT='[0-9]+,[^,]*,[0-9.]+' -v OFS='\n' '{$1=$1}1' file
1604999,Vancouver - BC,0.008000
1605,USA OUT,0.012500
1605397,USA,0.039000
1605475,,0.061000
1605477,,0.061000
1605692,,0.061000
1605715,,0.061000
1606,USA OUT,0.012500
1607,USA OUT,0.012500
1608,USA OUT,0.012500
任何awk:
$ awk -v ORS= '{gsub(/[0-9]+,[^,]*,[0-9.]+/,"&\n"); gsub(/\n /,"\n")}1' file
或者如果您更喜欢循环:
$ awk '{
while ( match($0,/[0-9]+,[^,]*,[0-9.]+/) ) {
print substr($0,RSTART,RLENGTH)
$0 = substr($0,RSTART+RLENGTH)
}
}' file
答案 1 :(得分:1)
$ awk -F' ' -v RS=, 'NR>1 && 1==NR%2{print $1; printf "%s,",$2; next} {printf "%s,",$0}' rates.csv
1201,New Jersey,0.012500
1202,USA OUT,0.012500
1203,USA OUT,0.012500
1204,Manitoba,0.008000