抓取标签

抓取标签

我从已处理的查询中获得以下详细信息:

queuename                      qtype resv/used/tot. np_load  arch          states
---------------------------------------------------------------------------------
abax55@lp55cs008               BP    0/36/36        1.08     lx-amd64      
    gf:app_monitor=1
    gf:app_abaqus=1
    gf:app_abaqusfgs=1
    gf:app_actran=1
    hl:load_avg=38.980000
    hl:load_short=38.550000
    hl:load_medium=38.980000
    hl:load_long=39.030000

我只想hl:load_avg=38.980000使用前缀进行grep abax55@lp55cs008 ,这意味着输出应如下所示:

abax55@lp55cs008 - hl:load_avg=38.980000

....这仅适用于一台名为 的机器cs008。机器总数将超过100台。

请建议 2 个选项:

  1. 仅 grep 特定机器,
  2. 用于 grep 所有机器

答案1

Awk解决方案:

1) 仅用于 grep 特定机器:

awk -v m="cs008" '/abax55@lp55cs[0-9]/ && $1 ~ m{ m_name=$1 }
       m_name && /hl:load_avg=/{ print m_name" - "$1; exit }' file

输出:

abax55@lp55cs008 - hl:load_avg=38.980000

2)用于 grep 所有机器:

awk '/abax55@lp55cs[0-9]/{ m_name=$1 }
     m_name && /hl:load_avg=/{ print m_name" - "$1; m_name=0 }' file

答案2

为了测试,我通过更改文件中的主机名多次在文件中插入相同的内容

input file

queuename                      qtype resv/used/tot. np_load  arch          states
---------------------------------------------------------------------------------
abax55@lp55cs008               BP    0/36/36        1.08     lx-amd64
    gf:app_monitor=1
    gf:app_abaqus=1
    gf:app_abaqusfgs=1
    gf:app_actran=1
    hl:load_avg=38.980000
    hl:load_short=38.550000
    hl:load_medium=38.980000
    hl:load_long=39.030000
queuename                      qtype resv/used/tot. np_load  arch
states
---------------------------------------------------------------------------------
abax55@lp55cs009               BP    0/36/36        1.08     lx-amd64
    gf:app_monitor=1
    gf:app_abaqus=1
    gf:app_abaqusfgs=1
    gf:app_actran=1
    hl:load_avg=38.980000
    hl:load_short=38.550000
    hl:load_medium=38.980000
    hl:load_long=39.030000
queuename                      qtype resv/used/tot. np_load  arch
states
---------------------------------------------------------------------------------
abax55@lp55cs007               BP    0/36/36        1.08     lx-amd64
    gf:app_monitor=1
    gf:app_abaqus=1
    gf:app_abaqusfgs=1
    gf:app_actran=1
    hl:load_avg=38.980000
    hl:load_short=38.550000
    hl:load_medium=38.980000
    hl:load_long=39.030000

命令

 for i in `sed -n '/abax55@lp55cs/p' file.txt  |awk '{print $1}'`; do sed -n "/$i/,+5p" file.txt  | awk '{print $1}' | sed -n  -e '1p' -e '$p' | perl -pne "s/\n/-/"| sed 's/-$/\n/g'; done

输出

abax55@lp55cs008-hl:load_avg=38.980000
abax55@lp55cs009-hl:load_avg=38.980000
abax55@lp55cs007-hl:load_avg=38.980000

答案3

我不会使用grep我会使用perl

#!/usr/bin/env perl
use strict;
use warnings;

use Data::Dumper;

my %values = do { local $/; <> } =~ m/(\w+\@\w+).*?hl:load_avg=([\d\.]+)/gms; 

print Dumper \%values; 

print $values{'abax55@lp55cs008'}

如果您能够提供更具体的记录格式,我可能可以改进它。但是您可以轻松地将整个节解析为一个块,因为每个新的“块”在开头都没有空格(如果记录之间有明确的分隔符,例如空行,但没有看到您的输出,那就更容易了不知道那会是什么)。

#!/usr/bin/env perl
use strict;
use warnings;

use Data::Dumper;

my $current_host = ''; 
my %values; 

#<> reads stdin or files specified on command line.
#if that doesn't work for you, you can "open" a specific file, or 
#use qx() or backticks to run a a command. 
while ( <> ) {
   #match a regex and capture the result if it's valid. 
   if ( m/^(\w+\@\w+)/ ) { $current_host = $1 }; 

   #lines that start with whitespace are the values. 
   if ( m/^\s+(\w+:\w+)=([\d\.]+)/ ) {
      #$1 and $2 are captured via the brackets
      $values{$current_host}{$1} = $2; 
   }
}

#for debug:
print Dumper \%values;

#to print a specific key:

my $target_key = 'hl:load_avg'; 

foreach my $host ( sort keys %values ) {
   print "$host - $target_key=",$values{$host}{$target_key},"\n";
}

相关内容