8

How can I redirect output of mysql query with table border as separator? I try with this code :

mysql -u root -h localhost -D cronjob -e "select * from cron_list" > query.txt

What I expect is output like :

| id |  datetime  | recurrency |          command          |
|  1 | 2014-10-10 |    daily   | bash /media/data/daily.sh |
|  2 | 2014-10-09 |  minutely  |           conky           |

But when I use that code, it give me output like

 id   datetime   recurrency           command          
  1  2014-10-10     daily    bash /media/data/daily.sh 
  2  2014-10-09   minutely             conky           

So, how can I redirect the output from terminal into file with separator like in terminal? Sorry for bad english

1
  • 3
    Try using -t parameter. Commented Oct 10, 2014 at 8:37

1 Answer 1

12

From man mysql:

--table, -t

Display output in table format. This is the default for interactive use, but can be used to produce table output in batch mode.

Note -t is the default behaviour, but not whenever you pipe to a file. That's why in that case you have to make it explicit.


Other interesting options, for other kind of outputs:

--silent, -s

Silent mode. Produce less output. This option can be given multiple times to produce less and less output.

This option results in nontabular output format and escaping of special characters. Escaping may be disabled by using raw mode; see the description for the --raw option.

--skip-column-names, -N

Do not write column names in results.

--html, -H

Produce HTML output.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't know, when I didn't include -t parameter. It give me output like --silent. By the way, thanks for explanation :)
I have been looking for information of default settings but I cannot find anywhere that sets these defaults. Just guessing: maybe you have an alias?
"...produce table output in batch mode" appears to mean "whenever STDOUT is not a tty," as is the case with > redirection to a file.
@Michael-sqlbot excellent! This seems to answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.