Ways to export MySQL result set to file on client side
Problem: you wish to write table data to file, but you wish to do so on client side.
SELECT ... INTO OUTFILE writes the file on server. What are your options on client?
More options include the --xml or --html formats.
This means result sets are written in table format (what with all the +---+-----+---+ frames), which is not most convenient to parse later on.
pager + unix command open an endless gateway of opportunities. Use awk to get rid of table frames. Use grep to filter frame rows out. What have you.
SELECT ... INTO OUTFILE writes the file on server. What are your options on client?
1. mysql client
If you have direct access from your client machine to your DB server machine, and can connect via mysql client, you get a very customizable file write:The above writes fancy tables, so you probably want to:bash$ mysql -h db_host --execute "SELECT * FROM my_table" > /tmp/output.txt
Also try:bash$ mysql -h db_host --execute "SELECT * FROM my_table" --bat > /tmp/output.txt
To turn off headers.bash$ mysql -h db_host --execute "SELECT * FROM my_table" --silent --raw > /tmp/output.txt
More options include the --xml or --html formats.
2. tee
tee works similarly to a unix tee command. From the mysql command line, issue:Anything you type from that moment on, and anything returned from the server, are written to given file, in the same format by which they are displayed on your client.mysql> tee file_name.txt
This means result sets are written in table format (what with all the +---+-----+---+ frames), which is not most convenient to parse later on.
3. pager
If you're on unix/linux, you have a third option: use the pager command to write result sets to file. For example, use:This will work similarly to the tee command, but will not verbose to screen. In the above we append results to file.mysql> pager cat - >> /tmp/pager_output.txt
4. pager, Unix tee
So, while we're at it, one can:The above will rewrite the file for every result set. Play at your own leisure to generate different files. For example, usemysql> pager tee /tmp/tee_file.txt
for timestamp signature.mysql> pager tee /tmp/tee_$(date +%M%H)_file.txt
pager + unix command open an endless gateway of opportunities. Use awk to get rid of table frames. Use grep to filter frame rows out. What have you.
0 comments:
Post a Comment