MySQL output as CSV, on command line
Maybe it's because MySQL's default output (to screen or file) seemed quite friendly to begin with that I never thought about doing this before, but it's easy to get MySQL's output as CSV (other than using mysqldump with several arguments).
# add alias to .bashrc
alias mysql2csv='sed '\''s/\t/","/g;s/^/"/;s/$/"/;s/\n//g'\'''
Then use like this:
$ mysql <usual args here> -e "SELECT * FROM foo" | mysql2csv > foo.csv
Written by Michael Jemmeson
Related protips
1 Response
This is actually really useful. I have my own similar sed command for this, but wrapping it up into an alias is a great idea.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Mysql
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#