Exporting query details to csv file

Founded somewhere in the net. Really useful.
From the Unix commandline (Bourne Shell compatible):

output tab-seperated: 
echo "select * from example;" | mysql -u user -h host -ppass 

Want another delimeter? Add  
| tr '^V^I' ';' 
to the pipe and you will get csv or any other delimeter you want.

Skipping the first row (Table Header): add 
| tail -n +2 

Want DOS-CRLF? Simply add
| sed -e 's/$/^V^M/g'

Write to a file:  add 

Please Note: to get ^M (CR) or ^I(tab) as a single(!) control-character
(in vi or bash), you have to type [Ctrl]+[V] [Ctrl]+[M] (or [I])

  1. so what’s wrong with mysql -sB options?

  2. lol, seems to do the trick!

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>