Change output format for MySQL command line results to CSV

前端 未结 7 2069
囚心锁ツ
囚心锁ツ 2020-12-05 03:54

I want to get headerless CSV data from the output of a query to MySQL on the command line. I\'m running this query on a different machine from the MySQL server, so all those

7条回答
  •  没有蜡笔的小新
    2020-12-05 04:25

    The above solutions only work in special cases. You'll get yourself into all kinds of trouble with embedded commas, embedded quotes, other things that make CSV hard in the general case.

    Do yourself a favor and use a general solution - do it right and you'll never have to think about it again. One very strong solution is the csvkit command line utilities - available for all operating systems via Python. Install via pip install csvkit. This will give you correct CSV data:

        mysql -e "select people, places from things" | csvcut -t
    

    That produces comma-separated data with the header still in place. To drop the header row:

        mysql -e "select people, places from things" | csvcut -t | tail -n +2
    

    That produces what the OP requested.

提交回复
热议问题