简体   繁体   English

Grep Hive输出日志中的结果

[英]Grep a result from Hive output log

I have an output from Hive. 我有Hive的输出。 I stored that output in a variable called match . 我将该输出存储在一个名为match的变量match

I am isolating the line I need from the log using the command below. 我使用以下命令从日志中隔离所需的行。

echo $(echo $match | grep "COUNT_TOTAL_MATCH")

0: jdbc:hive2://hiveaddress> . . . . . . . . . . . . . . . . . . . . . . .> +--------------------+-------+--+ | stats | _c1 | +--------------------+-------+--+ | COUNT_TOTAL_MATCH | 1000 | +--------------------+-------+--+ 0: jdbc:hive2://hiveaddress> 0: jdbc:hive2://hiveaddress>

How do I grab the 1000 value knowing it could be any other number? 知道可能是其他任何数字,我该如何获取1000值?

You can treat | 您可以治疗| (space pipe space) as the field delimiter and print the sixth field, like this: (空间管道空间)作为字段定界符,并打印第六个字段,如下所示:

awk -F ' \\| ' '{ print $6 }'

Notice that the pipe has to be escaped twice . 请注意,必须将管道放空两次


Side note: 边注:

echo $(echo $match | grep "COUNT_TOTAL_MATCH")

can be rewritten as 可以改写成

grep 'COUNT_TOTAL_MATCH' <<< "$match"

No echo , no pipes, and no word splitting in $match . $match没有echo ,没有管道,也没有单词分裂。 echo "$(command)" is always the same as just command . echo "$(command)"始终与just command相同。 (Notice that quoting makes a difference, though.) (但是请注意,报价会有所不同。)

This means that you can combine your grep and awk commands into this: 这意味着您可以将grep和awk命令合并为:

awk -F ' \\| ' '/COUNT_TOTAL_MATCH/ { print $6 }' <<< "$match"

try 尝试

grep -oP 'COUNT_TOTAL_MATCH\h*\|\h*\K\d+'
  • \\h*\\|\\h* optional space/tab followed by | \\h*\\|\\h*可选空格/制表符,后跟| followed by optional space/tab 后跟可选的空格/制表符
  • \\K is positive lookbehind... so only if COUNT_TOTAL_MATCH\\h*\\|\\h* is matched \\K是正向COUNT_TOTAL_MATCH\\h*\\|\\h* ...因此,只有COUNT_TOTAL_MATCH\\h*\\|\\h*
    • \\d+ get digits \\d+获取数字

From man grep 来自man grep

   -o, --only-matching
          Print  only  the matched (non-empty) parts of a matching line, with each such part on a separate output
          line.

   -P, --perl-regexp
          Interpret  the pattern as a Perl-compatible regular expression (PCRE).  This is highly experimental and
          grep -P may warn of unimplemented features.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM