我想像这样做一个 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
这是臭名昭著的flip-flop .
关于python - 我如何在 awk/start/,/stop/这样的 ruby 中做一个范围正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12980500/