【问题标题】:extract/split from string in bash从bash中的字符串中提取/拆分
【发布时间】:2014-11-27 21:01:40
【问题描述】:

我在 bash 脚本中有一组命令,例如在 impala 中运行查询。

#! /bin/sh
RESULT=`impala-shell -q "select count(*) from testable"`

shell脚本的结果是。

Starting Impala Shell without Kerberos authentication
Connected to mycomp.cool-cluster.com:21000
Server version: impalad version 2.0.0-cdh5 RELEASE (build 12323323)
Query: select count(*) from enriched_call
Fetched 1 row(s) in 0.62s
+----------+ | count(*) | +----------+ | 234343 | +----------+

我只想从此输出中获取值,即 234343,并尝试使用正则表达式、拆分等在脚本中断言计数。

如果我尝试消除非数字值,则版本中的数字也会出现。我想用“|”分割并获取值234343

【问题讨论】:

    标签: regex bash split


    【解决方案1】:

    尝试这样做:

    RESULT=$(
        impala-shell -q "select count(*) from testable" |
            awk -F'|' 'END{print $4}'
    )
    

    【讨论】:

      【解决方案2】:
      RESULT=$( impala-shell -q "..." | tail -n 1 | grep -o '[[:digit:]]\+' )
      

      RESULT=$( impala-shell -q "..." | sed -n '$s/[^[:digit:]]//gp' )
      

      【讨论】:

        猜你喜欢
        • 2016-05-30
        • 1970-01-01
        • 2014-04-16
        • 2013-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多