如何使用 mysqlimport 解析使用特殊字符作为分隔符的文档?

发布于 2024-11-03 14:10:51 字数 490 浏览 2 评论 0原文

我已经尝试过:

 mysqlimport --local  --fields-optionally-enclosed-by='\x254' 
             --fields-terminated-by='\x14' testdb  messages.txt 

以及:

 mysqlimport --local  --fields-optionally-enclosed-by='\xFE' 
             --fields-terminated-by='\cT' testdb  messages.txt 

我得到:

mysqlimport:错误:1083,字段分隔符参数不是预期的;使用 table: messages 时请检查手册。

我尝试过双引号、无引号以及单引号,如上面所示。有人知道正确的语法应该是什么吗?

I've tried:

 mysqlimport --local  --fields-optionally-enclosed-by='\x254' 
             --fields-terminated-by='\x14' testdb  messages.txt 

as well as:

 mysqlimport --local  --fields-optionally-enclosed-by='\xFE' 
             --fields-terminated-by='\cT' testdb  messages.txt 

and I get :

mysqlimport: Error: 1083, Field separator argument is not what is expected; check the manual, when using table: messages.

I've tried double quotes, no quotes as well as single quotes like above. Anyone know what the correct syntax should be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同展鸳鸯锦 2024-11-10 14:10:51

根据文档(http://dev.mysql.com/ doc/refman/5.0/en/load-data.html),只有一些转义序列可用于 mysqlimport,这些是:

Character   Escape Sequence
\0  An ASCII NUL (0x00) character
\b  A backspace character
\n  A newline (linefeed) character
\r  A carriage return character
\t  A tab character.
\Z  ASCII 26 (Control+Z)
\N  NULL

因此,如果您遇到不寻常的行结尾(如“\x02\n”),您除了预格式化你别无选择 文件。幸运的是,只要使用一点 sed 就可以很容易地做到这一点。例如,对于以“\x02\n”结尾的行和以“\x01”结尾的字段,您可以使用以下 bash 脚本:

#!/bin/sh
FILE="$1.tmp"

# Copying
cp $1 $FILE
# Removing comments from file
sed -i '/^#/d' $FILE
# Replacing field separator
sed -i 's/\x01/\x00%/g' $FILE
# Replacing lineends
sed -i ':a;N;$!ba;s/\x02\n/\x00\n/g' $FILE

然后执行以下 mysqlimport

mysqlimport --fields-terminated-by="\0%" --lines-terminated-by="\0\n" [...] $FILE

应该可以很好地工作。

According to the documentation (http://dev.mysql.com/doc/refman/5.0/en/load-data.html), only some escape sequences are available with mysqlimport, those are :

Character   Escape Sequence
\0  An ASCII NUL (0x00) character
\b  A backspace character
\n  A newline (linefeed) character
\r  A carriage return character
\t  A tab character.
\Z  ASCII 26 (Control+Z)
\N  NULL

Therefore, if you got unusual line endings (like "\x02\n"), you have no other choice than pre formatting your file. Luckily, it's pretty easy with a bit of sed. For example, with a line ending "\x02\n" and field ending "\x01", you may use the following bash script :

#!/bin/sh
FILE="$1.tmp"

# Copying
cp $1 $FILE
# Removing comments from file
sed -i '/^#/d' $FILE
# Replacing field separator
sed -i 's/\x01/\x00%/g' $FILE
# Replacing lineends
sed -i ':a;N;$!ba;s/\x02\n/\x00\n/g' $FILE

Then shot the following mysqlimport

mysqlimport --fields-terminated-by="\0%" --lines-terminated-by="\0\n" [...] $FILE

Shall work nicely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文