0

Using this:

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

That works fine until the config has the IPs on following lines. How can I get it to print the IPs if they are on following lines. The config has both, some have it on the same line, some on then next 1, 2 or 3 lines.

the data :

    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
          }
       }
}
4

2 回答 2

1

Try the following awk code:

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
     }               
 }'

That will print the IP lines while they exists.

于 2012-10-17T16:29:05.863 回答
0

It's hard to say without seeing more of your data and your expected output but I think all you need is something like this:

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

The above should be a good starting point at least. wrt the other answer posted using getline - getline has some appropriate uses but this isn't one of them, don't use getline until you fully understand and can live with all of it's caveats, see http://awk.info/?tip/getline.

于 2012-10-17T16:55:24.273 回答