[英]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.