Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing MySQL output in Bash

Tags:

bash

mysql

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

like image 540
Lazuardi N Putra Avatar asked Oct 10 '14 08:10

Lazuardi N Putra


1 Answers

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.

like image 101
fedorqui 'SO stop harming' Avatar answered Oct 13 '22 17:10

fedorqui 'SO stop harming'