我需要在导入数据仓库服务器之前将mysql转储文件转换为CSV格式。
INSERT INTO `temp` VALUES (30686631,1346959848246,1346959850865,1346959998054,'18663196147','18663196147','18668839208','17326812123',3372579,'1866319614700','A',1,'','',0,147,30686632,'KeyAd','1101','38.325.Monitor2.1101@10.40.10.170','10.40.10.40',5060,'10.40.10.46',5060,'100038455383251101_Monitor2@10.40.10.170','<sip:+18668839208@10.40.10.46:5060>;tag=sansay507370834rdb810','\"O\'HALLORAE,AEAN\" <sip:+17326812123@10.40.10.40;isup-oli=00>;tag=sansay507370829rdb1779','200',0,'',0,NULL,'','',3398812,NULL,NULL);
我正在使用此命令删除mysql插入语句
sed -e 's/^INSERT INTO `temp` VALUES (//' -e 's/);$//' -e 's/(//;s/);//;s/,/|/g;s|["'\'']||g'
当名字出现在两个斜线之间时似乎存在问题\ \,我无法弄清楚如何修复它。
从MySQL插入
'\"O\'HALLORAE,AEAN\"
无法弄清楚如何将输出形成
"O'HALLORAN,SEAN"
Desierd输出:
30686631|1346959848246|1346959850865|1346959998054|18663196147|18663196147|18668839208|17326812123|3372579|1866319614700|A|1|||0|147|30686632|KeyAd|1101|38.325.Monitor2.1101@10.40.10.170|10.40.10.40|5060|10.40.10.46|5060|100038455383251101_Monitor2@10.40.10.170|<sip:+18668839208@10.40.10.46:5060>;tag=sansay507370834rdb810| "O'HALLORAN,SEAN" <sip:+17326812123@10.40.10.40;isup-oli=00>;tag=sansay507370829rdb1779|200|0||0|NULL|||3398812|NULL|NULL
答案 0 :(得分:1)
试试这个:
$ sed -e 's/INSERT INTO `temp` VALUES (//' -e 's/);$//' -re 's/("[^"]*),([^"]*")/\1\x1\2/g;s/,/|/g;s/\x1/,/g;s/\\([^\])/\1/g' file | sed "s/'|/|/g;s/|'/|/g"
输出:
30686631|1346959848246|1346959850865|1346959998054|18663196147|18663196147|18668839208|17326812123|3372579|1866319614700|A|1|||0|147|30686632|KeyAd|1101|38.325.Monitor2.1101@10.40.10.170|10.40.10.40|5060|10.40.10.46|5060|100038455383251101_Monitor2@10.40.10.170|<sip:+18668839208@10.40.10.46:5060>;tag=sansay507370834rdb810|"O'HALLORAN,SEAN" <sip:+17326812123@10.40.10.40;isup-oli=00>;tag=sansay507370829rdb1779|200|0||0|NULL|||3398812|NULL|NULL
答案 1 :(得分:0)
如果ruby是一个可接受的依赖项,如果可以将语句转换为有效的ruby数组,则可以利用它的解析器:
script.sh
:
#!/bin/bash
# -r to preserve backslashes
read -r statement
ruby=$(echo -n $statement | sed -e 's/^.*VALUES //' -e 's/;$//' -e 's/^(/[/' -e 's/)$/]/' -e 's/NULL/"NULL"/g' -e 's/\\"/"/g')
echo $ruby | ruby -rcsv -e 'puts CSV.generate_line(eval($stdin.read), "|")'
用法:
chmod +x script.sh
echo <your statement> | ./script.sh
30686631|1346959848246|1346959850865|1346959998054|18663196147|18663196147|18668839208|17326812123|3372579|1866319614700|A|1|""|""|0|147|30686632|KeyAd|1101|38.325.Monitor2.1101@10.40.10.170|10.40.10.40|5060|10.40.10.46|5060|100038455383251101_Monitor2@10.40.10.170|<sip:+18668839208@10.40.10.46:5060>;tag=sansay507370834rdb810|"""O'HALLORAE,AEAN"" <sip:+17326812123@10.40.10.40;isup-oli=00>;tag=sansay507370829rdb1779"|200|0|""|0|NULL|""|""|3398812|NULL|NULL
这在openoffice上按预期加载(在将分隔符设置为“|”之后)