Last Updated: February 25, 2016
·
8.124K
· mjemmeson

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

1 Response
Add your 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 ·