[英]awk script, need to return results on multi lines following keyword
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. 直到配置在以下行中具有IP为止,该方法都可以正常工作。 How can I get it to print the IPs if they are on following lines. 如果IP位于以下行中,我如何获得它来打印IP。 The config has both, some have it on the same line, some on then next 1, 2 or 3 lines. 该配置同时具有,有些在同一行上,有些在随后的1、2或3行上。
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
}
}
}
Try the following awk
code: 尝试以下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
}
}'
That will print the IP lines while they exists. 这将在IP线存在时打印它们。
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 . 使用getline发布的其他答案-getline有一些适当的用途,但这不是其中之一,在您完全理解并可以忍受所有警告之前,不要使用getline,请参阅http://awk.info/?提示/ getline 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.