thejof.com
hacks and main29 Sep 2006 03:34 pm

If perhaps you’d like to export some MySQL data as csv values, here’s a little sed hack I figured out this afternoon:

[jonathan@magnar:~]$ mysql -umysqluser -ppassword dbname -B -e “select * from tablename order by foo;” | sed ’s/\t/,/g;’ > somefile
That sed block might be a little scary to some newbies, but it replaces tabs (\t) with a comma.
That’ll give data in the format of:

foo,bar
baz,gazonk

Sometimes, quotes are more appropriate for some automated csv imports, so use this instead:

[jonathan@magnar:~]$ mysql -umysqluser -ppassword dbname -B -e “select * from tablename order by foo;” | sed ’s/\t/,/g;s/^/”/;s/\n//g’ > somefile

That’ll make data in the form of:

“foo”,”bar”
“baz”,”quux”

Trackback this Post | Feed on comments to this Post

Leave a Reply