Tuesday, March 23, 2010

Matching Numeric Ranges with Ruby

regular-expressions.info have dedicated a page to how this can be accomplished using regular expressions alone. This approach is limited in the ranges that can be matched and the patterns add to the complexity of the expression needed to match desired lines.

Realizing this shortcoming of a pure regex approach, I decided to follow a programmatic one with ruby. Here is a command that will display Java VM (run with -verbose:gc) Garbage Cleaning output with a pause time of between 2 and 5 seconds.

cat ./logs/catalina.out | ruby -e 'STDIN.each {|line| puts line if line =~ /\[GC [0-9]+K->[0-9]+K\([0-9]+K\), ([0-9]+\.[0-9]+) secs\]/ and (2..5).include?($1.to_f)}'
Ruby receives a standard input a line at a time and checks it against the regular expression. If a line matches, the part of the pattern enclosed by un-escaped parentheses is made available in the variable $1. That variable is converted to float and compared to the given range. The line is then printed to standard output if the value falls within the range.

No comments:

Post a Comment