解析 mysqldump 的输出以创建带有字段名称标题的 CSV 文件
我正在尝试编写一个 bash 脚本,根据本地 MySQL 数据库的名称,该脚本将其所有表中的数据导出到 CSV 文件中,该文件的标题行包含字段名称。例如,如果数据库有表customers
、orders
和inventory
,我想转储三个文件customers.csv、
orders.csv
和 inventory.csv
,包括每个文件标题中的字段名称。
通过修改 mysqldump,我成功生成了所需的所有 .csv 文件,但没有字段名称标题行。该命令还在 SQL 命令中创建一组仅包含表结构的 .sql 文件。在谷歌搜索了很多之后,我找不到任何人能够在不“重新发明轮子”并编写自己的 MySQL 转储脚本的情况下解决这个问题。 mysqldump 很棒,只是缺少这个小功能。我需要的所有字段名称都在这些 SQL 文件中,只需将它们解析出来并在每个 CSV 文件前面添加一行字段名称,对吗?
我的问题:我是一个 shell 脚本新手,我不知道如何解决这个问题。
这是我当前使用的 mysqldump 命令:
mysqldump --host=localhost --user=myusername --password=mypassword \
--tab=/tmp/db/ --verbose mydatabase \
--fields-enclosed-by=\" --fields-terminated-by=,
假设数据库 mydatabase 有表 customers
orders
和 inventory
。此命令将在目录 /tmp/db 中生成六个文件:customers.sql
、customers.txt
、orders.sql
、order.txt
、inventory.sql
、inventory.txt
。 (txt 文件是 CSV 文件,我的脚本稍后将文件扩展名更改为 .csv)
.sql 文件看起来像这样(以客户为例):
-- MySQL dump 10.13 Distrib 5.1.54, for debian-linux-gnu (i686)
--
-- Host: localhost Database: mydatabase
-- ------------------------------------------------------
-- Server version 5.1.54-1ubuntu4
/* (i removed some generated comments here) */;
--
-- Table structure for table `customers`
--
DROP TABLE IF EXISTS `customers`;
/* (i removed some generated comments here) */;
CREATE TABLE `customers` (
`customer_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(256) NOT NULL,
`last_name` varchar(256) NOT NULL,
`email` varchar(256) NOT NULL,
`phone` varchar(20) NOT NULL,
`notes` longtext NOT NULL,
PRIMARY KEY (`customer_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1865 DEFAULT CHARSET=latin1;
/* (i removed some generated comments here) */;
-- Dump completed on 2011-05-01 13:03:02
.txt 文件看起来就像您期望的 CSV 一样(行“data”、“like”、“this”),但没有字段名称标题。
如您所见,我需要的字段名称就位于“CREATE TABLE...”之后的 .sql 文件中。
我尝试编写的理想脚本将执行以下操作:
- 运行上面详细介绍的 mysqldump 命令。
- 循环遍历所有匹配 /tmp/db/*.sql 的文件,并对每个文件:
- 解析出字段名称并生成“string”、“of”、“them”、“like”、“this”
- 在匹配的 .txt 文件中,在第一行之前插入字段名称字符串。
- 将所有 .txt 文件重命名为 .csv 并删除所有 .sql 文件。
有什么建议吗?我会整天修补这个问题,直到弄清楚为止。
I'm trying to write a bash script which will, given the name of a local MySQL database, export the data from all its tables into CSV files with a header line containing field names. For example, if the database has tables customers
, orders
, and inventory
, i want to dump three files customers.csv
, orders.csv
and inventory.csv
, including the field names in headers of each file.
Tinkering with mysqldump, I've managed to produce all the .csv files I need, but without the field name header line. The command also creates a set of .sql files containing only the table structure, in SQL commands. After googling around a lot, I can't find anyone who's been able to solve this problem without "reinventing the wheel" and writing their own MySQL dump script. mysqldump is great, it just lacks this one little feature. And all the field names I need are right there in those SQL files, it should just be a matter of parsing them out and prepending a line of field names to each CSV file, right?
My problem: I'm a shell scripting newbie, and I have no idea how to go about this.
Here's the mysqldump command I'm currently using:
mysqldump --host=localhost --user=myusername --password=mypassword \
--tab=/tmp/db/ --verbose mydatabase \
--fields-enclosed-by=\" --fields-terminated-by=,
let's say the database mydatabase has the tables customers
orders
and inventory
. This command will produce six files in the directory /tmp/db: customers.sql
, customers.txt
, orders.sql
, orders.txt
, inventory.sql
, inventory.txt
. (the txt files are CSV files, my script later changes the file extension to .csv)
The .sql files look like this (using customers as an example):
-- MySQL dump 10.13 Distrib 5.1.54, for debian-linux-gnu (i686)
--
-- Host: localhost Database: mydatabase
-- ------------------------------------------------------
-- Server version 5.1.54-1ubuntu4
/* (i removed some generated comments here) */;
--
-- Table structure for table `customers`
--
DROP TABLE IF EXISTS `customers`;
/* (i removed some generated comments here) */;
CREATE TABLE `customers` (
`customer_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(256) NOT NULL,
`last_name` varchar(256) NOT NULL,
`email` varchar(256) NOT NULL,
`phone` varchar(20) NOT NULL,
`notes` longtext NOT NULL,
PRIMARY KEY (`customer_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1865 DEFAULT CHARSET=latin1;
/* (i removed some generated comments here) */;
-- Dump completed on 2011-05-01 13:03:02
and the .txt files look as you would expect a CSV to look (lines of "data","like","this") but with no field name headers.
As you can see, the field names I need are right there in the .sql files after "CREATE TABLE...".
The ideal script I'm trying to write would do the following:
- run the mysqldump command i detailed above.
- loop through all files matching /tmp/db/*.sql, and for each one:
- parse out the field names and generate a "string","of","them","like","this"
- in the matching .txt file, insert the string of field names before the first line.
- rename all .txt files as .csv and delete all .sql files.
Any tips? I'll be tinkering with this all day until I figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在windows下做了一个简单的测试。
这是我的 csv_header.txt 的内容:
如果这就是您正在寻找的内容,那么创建一个带有游标的存储过程很简单,该游标循环架构中的所有表并对每个表执行相同的操作。让我知道。 :)
I made a simple test under windows.
And this is the content of my csv_header.txt:
If this is what you're looking for, it's simple to create a stored procedure with a cursor that loops all the tables within the schema and do the same thing for each one. Let me know. :)
我找不到一种方法来做到这一点,因为:
我执行了标准 SQL 转储并将文件转换为 CSV 格式。
I couldn't find a way to do this, given that:
I did a standard SQL dump and grep'd the file into CSV format.