Welcome

Exporting a MySQL table to a CSV file

A little trick I found on the net. The example I found was wrong, so I will show the working version. First the original web page:

http://www.debianadmin.com/…

Now the meat:
mysql database -e "SELECT * FROM table" | sed 's/\t/","/g;s/^/"/;s/$/"/' > yourfile.csv

A little explanation. When you use “-e” with mysql there is an implicit “-B”. That means your output is delimited with tabs. Then sed does these things:

  1. Change all the tabs to “,”
  2. Add a ” to the beginning of each line
  3. Add a ” to the end of each line

Voila! You have a CSV file. Have a ball.

Actions: Trackback URL for this entry Commentfeed

Leave a comment

You must be logged in to post a comment.