MySQL命令行垂直输出

8

我想要通过垂直输出的方式获取mysql查询的结果。 使用--vertical(或G)时,我的问题在于星号行。

    $mysql -N -e 'select filed1, field2 from db.tlb\G'
    *************************** 1. row ***************************
    value1_field1
    value1_field2
    *************************** 2. row ***************************
    value2_field1
    value2_field2
    ...

有没有什么选项可以去除行中的 ***[...] x. row [...]*** ?

目前,我使用的是 egrep -v '^\*.*\*$',但我相信一定有更好的解决方案。


1
我认为你的方法可能是最好的 - 我不相信有一种选项可以删除行线。其他更复杂的可能性是选择列作为标记化连接,然后将其管道传输到 sed 中以将标记转换为换行符。如果你已经有可用的方法可以工作,我建议坚持使用它。 - Michael Berkowski
+1 给你自己,也从我这里得到一个 :D - mirkobrankovic
3个回答

0

我不确定这是一个好的解决方案,但如果明确使用egrep很烦人,您可以定义一个shell函数来启动带有所需分页器mysql。假设您正在使用bash(或兼容):

# define a shell function to launch mysql with the required _pager_
sh$ mysql() { `which mysql` $* --pager="egrep -v '^\*.*\*$'" ; }

[ ... ]

# launch mysql "as usual" (in reality, this is the shell function that is invoked)
sh$ mysql -u user -p -h mysql-host mydb
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 67
Server version: 5.1.49-3 (Debian)

-- Use mysql normally -- but the "egrep pager" will remove the "star lines"
mysql> select * from T\G
col1: w
col2: x
col3: 0.4
1 row in set (0.00 sec)

正如我之前所说,这并不是一个完美的解决方案,因为egrep会盲目地从输出中删除任何“星号行”——不仅仅是来自ego\G)命令的那一行。


0

尝试这个(但我无法想象你为什么需要这样的输出)

mysql -N -B -e 'select * from db' mysql | tr "\t" "\n"

0

第一种选择

是更改MySQL分页器,如下所示:

test.sh

#!/usr/bin/env bash

# Some Basic Configuration
db="homestead"
dbuser="homestead"
dbhost="127.0.0.1"
# Executes the MySQL command using the basic configuration
# This also sets the MySql Pager to less -Sin to enable
# it also removes all lines starting with "*"
mysql -h $dbhost -u $dbuser -p --pager="grep -Ev '(^\*.*$)|(^$)' | less -Sin" $db

使用方法

首先编辑配置变量:

$ nano ./test.sh 

其次运行脚本

$ bash ./test.sh

第二个选择

在进入MySQL CLI之后更改分页器

$ mysql -u root -p -h 127.0.0.1 somedatabase
enter password:
mysql> pager grep -Ev '(^\*.*$)|(^$)' | less -Sin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接