Monday 29 August 2016

linux - Export a table as csv in mysql from shell script

I am trying to export a result set into a csv file and load it to mysql.




mysql -e "select *  from temp" > '/usr/apps/{path}/some.csv'


The out put file is not importable. It has the query, headers and bunch of unwanted lines. All I want is just the COMMA delimited VALUES in the file, so that I can import it back.



What did I try so far?




  1. Added | sed 's/\t/,/g' - Did not help

  2. Tried OUTFILE but it did not work.


  3. Tried SHOW VARIABLES LIKE "secure_file_priv" which gave null.



OUTFILE will not work for me because I get the error "The MySQL server is running with the --secure-file-priv option so it cannot execute this statement". I cannot edit the variable secure-file-priv. And it has a null value right now.



I get the file output as below image. I used the alias mysql2csv='sed '\''s/\t/","/g;s/^/"/;s/$/"/;s/\n//g'\'''



enter image description here

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...