繁体   English   中英

awk脚本,需要在关键字之后的多行中返回结果

[英]awk script, need to return results on multi lines following keyword

使用这个:

awk '$1 == "pool" { f=1; print $1,$2; next }         
     f == 1 { if ($1 == "pool") { print }               
              else if ($1 == "members") { print }   
              else if ($0 ~ /^}/) { f=0 }               
     }' bigip.conf

直到配置在以下行中具有IP为止,该方法都可以正常工作。 如果IP位于以下行中,我如何获得它来打印IP。 该配置同时具有,有些在同一行上,有些在随后的1、2或3行上。

数据 :

    pool pl_stage_xxx_microsites_9483 {
       monitor all tcp_half_open
       members {
          11.11.11.11:9483 {}
          11.22.22.22:9483 {
             session user disabled
          }
       }
}

尝试以下awk代码:

awk '
$1 == "pool" {
    f=1
    print $1,$2
    next
}         
f == 1 {
    if ($1 == "pool") {
        print
    }               
    else if ($1 == "members") {
        print
        getline
        while ($0 ~ "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}"){
            print
            getline
        }
     }   
     else if ($0 ~ /^}/) {
         f=0
     }               
 }'

这将在IP线存在时打印它们。

没有看到更多的数据和预期的输出就很难说,但是我认为您需要的是这样的东西:

awk '
/^}/ { inPool=0 }
$1 == "pool" { inPool=1; inMembers=0 }
inPool {
   if ($1 == "pool") {
      print $1, $2
      print
   }
   else if ($1 == "members") {
      inMembers = 1
   }
   if (inMembers) {
      print
   }
}
' file

以上至少应该是一个很好的起点。 使用getline发布的其他答案-getline有一些适当的用途,但这不是其中之一,在您完全理解并可以忍受所有警告之前,不要使用getline,请参阅http://awk.info/?提示/ getline

暂无
暂无

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

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