0

在导入数据仓库服务器之前,我需要将 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 insert 语句

sed -e 's/^INSERT INTO `temp` VALUES (//' -e 's/);$//' -e  's/(//;s/);//;s/,/|/g;s|["'\'']||g'

当名称出现在两个斜杠 \ \ 之间时,它们似乎存在问题,我不知道如何解决它。

从 MySQL 插入

'\"O\'HALLORAE,AEAN\"

无法弄清楚如何形成输出

"O'HALLORAN,SEAN"

期望的输出:

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
4

2 回答 2

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
于 2013-02-28T16:52:31.913 回答
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 上按预期加载(在将分隔符设置为“|”之后)

于 2013-02-28T16:23:26.813 回答