原始文件内容:
pool pool_ssh {
monitor all tcp
member 192.168.2.28:ssh
member 192.168.2.29:ssh
}
virtual virtual_ssh {
ip protocol tcp
pool pool_ssh
}
预期输出:
virtual_ssh pool_ssh 192.168.2.28:ssh
virtual_ssh pool_ssh 192.168.2.29:ssh
命令:
awk -v virtualStart=0 -v poolStart=0 '{if($0~/^pool/){poolStart=1;poolCurrent=$2};if($0~/member/ && poolStart==1 ){pool[poolCurrent]=pool[poolCurrent]" "$2};if($0~/\}/ && poolStart==1){poolStart==0};if($0~/^virtual/){virtualStart=1; virtualCurrent=$2};if($0~/pool/ && virtualStart==1 ){ virtual[virtualCurrent]=$2;};if($0~/\}/ && virtualStart==1){virtualStart=0}}END{for(i in virtual){split(pool[virtual[i]],member," ");for(j in member){print i,virtual[i],member[j]}}}' 文件名
另外一种思路:
1、去掉全部的换行符,
2、将 } 替换成 }\n,处理成一行的形式:
pool pool_ssh { monitor all tcp member 192.168.2.28:ssh member 192.168.2.29:ssh}
virtual virtual_ssh { ip protocol tcp pool pool_ssh }
3、然后在进行处理
阅读(1870) | 评论(0) | 转发(0) |