Saturday, 6 August 2016

MySql command line csv




Is there a way to use the following command to output in comma separated instead of tab separated.



echo "select col1, col2, col3 from newtable" | mysql --column-names > data.csv


It outputs the following.




col1    col2    col3   
1 Test Test 1
2 Test Test 2
3 Test Test 3


how do I output a comma seperated file.


Answer



Just use tr to replace tabulations by commas:




echo "select col1, col2, col3 from newtable" | mysql --column-names | tr "\t" ";" > data.csv

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...