【问题标题】:mysql command line to vertical outputmysql命令行到垂直输出
【发布时间】:2013-06-08 12:55:53
【问题描述】:

我想得到一个带有垂直输出的 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 '^\*.*\*$',但我确信存在更好的解决方案。

【问题讨论】:

  • 我认为您的方法可能是最好的方法 - 我不相信有删除行线的选项。其他更复杂的可能性是将您的列选择为标记化连接,然后通过管道输入sed 以将标记转换为换行符。如果你所拥有的对你有用,我会说坚持下去。
  • +1 给你自己和我:D

标签: mysql command-line


【解决方案1】:

第一个选项

将像这样更改 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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2015-07-07
        • 1970-01-01
        • 1970-01-01
        • 2013-06-10
        • 2015-12-11
        • 1970-01-01
        • 2012-10-29
        • 2018-06-16
        • 2013-01-18
        相关资源
        最近更新 更多