October 10, 2008

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])


Leave a Reply

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