Quick one-liner to export from a DB to CSV

February 12th, 2008   (232 views )

We recently received some data in an sqldump that we wanted in CSV format.

Here's how you can use sed to tranform it into CSV (from here). This should all be one line...

mysql -u user -p dbname -B -e "SELECT * from table;" | 
sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > filename.csv
Categories: hacks

Comments, Pingbacks:

Comment from: Ping [Visitor] Email · http://wolog.net/
Watch out for quotation marks in your data...
PermalinkPermalink 02/12/08 @ 20:16
Comment from: joe [Member] Email · http://josephhall.org/nqb2/
heh, true...
PermalinkPermalink 02/12/08 @ 23:29

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))
To prove that you are human, please type Turing in the space below

Previous post: Henry Jenkins: "Combating the Participation Gap: Why New Media Literacy Matters"Next post: Dice Binning Calculator