MYSQL-E and awk to export CSV files together

Source: Internet
Author: User

Clevercode found a simple way to export MySQL data into CSV format.

1) MYSQL-E usage profile

Instructions for use: Execute an SQL statement and exit.

Format examples:

1) mysql-uroot-p123456-h 192.168.10.210-p 3306 db_logs-e "show databases;"
2) mysql-uroot-p123456-h 192.168.10.210-p 3306 db_logs-e "Select Cdate,uid,clientip,createtime from User_login_log Lim it 1; "

Description

-u:root is the user name;
-p:123456 for password
-h:mysql Server IP
-p:3306mysql Server Port
Db_logs: The database where the statement is executed
-E: The statement that needs to be executed.


2) Execute statements directly

# mysql-uroot-p123456-h 192.168.10.210-p 3306 db_logs-e "Select Cdate,uid,clientip,createtime from User_login_log Lim it 1; "



3) Save execution statement to file

# mysql-uroot-p123456-h 192.168.10.210-p 3306 db_logs-e "Select Cdate,uid,clientip,createtime from User_login_log Lim  it 1; " > Log.txt

# Cat Log.txt

You can see that the saved result is \ t delimited.



4) Save the execution statement to the file and use awk to replace \ t into a comma# mysql-uroot-p123456-h 192.168.10.210-p 3306 db_logs-e "Select Cdate,uid,clientip,createtime from User_login_log Lim It 1; "| awk ' {print $ ', ', $ $ ', ' $ $ ', ' $4} ' > Log.csv
# Cat Log.csv





MYSQL-E and awk to export CSV files together

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.