MySQL to CSV command line

Sometimes I need to dump the contents of a query out to a CSV file for reporting. Here’s the best method I’ve found thus far to do this on the command line.

mysql --user=wibble --password wobble -B -e "select * from vehicle_categories;" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > vehicle_categories.csv
Tagged with: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*