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 行的选项。排 [...]*** ?

目前,我正在使用egrep -v '^\*.*\*$',但我确信存在更好的解决方案。

4

3 回答 3

0

我不确定这是一个好的解决方案,但如果显式使用egrep很烦人,您可以定义一个 shell 函数以mysql使用所需的pager启动。假设您使用的是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) 命令的那个。

于 2013-06-27T07:47:00.040 回答
0

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

mysql -N -B -e 'select * from db' mysql | tr "\t" "\n"
于 2014-11-11T18:05:03.673 回答
0

第一个选项

将像这样更改 MySQL 寻呼机:

测试.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
于 2016-11-16T08:29:23.797 回答