我想做一个 AWK 风格的范围正则表达式,如下所示:
awk ' /hoststatus/,/\}/' file
在 AWK 中,这将打印文件中两个模式之间的所有行:
hoststatus {
host_name=myhost
modified_attributes=0
check_command=check-host-alive
check_period=24x7
notification_period=workhours
check_interval=5.000000
retry_interval=1.000000
event_handler=
}
我该如何在 Ruby 中做到这一点?
额外奖励:你会如何用 Python 做到这一点?
这在 AWK 中确实很强大,但我是 Ruby 新手,不确定你会如何做到这一点。在Python中我也找不到解决方案。
Ruby:
str =
"drdxrdx
hoststatus {
host_name=myhost
modified_attributes=0
check_command=check-host-alive
check_period=24x7
notification_period=workhours
check_interval=5.000000
retry_interval=1.000000
event_handler=
}"
str.each_line do |line|
print line if line =~ /hoststatus/..line =~ /\}/
end
这就是臭名昭著的拖鞋 https://stackoverflow.com/questions/1111286/when-would-a-ruby-flip-flop-be-useful.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)