Created
December 16, 2011 20:15
-
-
Save bitprophet/1487768 to your computer and use it in GitHub Desktop.
How to quickly test pure-Ruby Grok pattern strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Obtain pure-Ruby lib | |
require 'grok-pure' | |
# Create grok instance | |
grok = Grok.new | |
# Map some regex patterns to pattern names; this sets up %{FOO} | |
grok.add_pattern("FOO", "[abc]") | |
# And/or load from file -- highly useful is the patterns/grok-patterns file that ships with Logstash source | |
# grok.add_patterns_from_file('/path/to/pattern/file') | |
# Actually tell it what you want to search for | |
grok.compile("lol %{FOO:foo1} %{FOO:foo2}") | |
# Finally, test that compiled pattern against input strings | |
result = grok.match("lol wut") | |
# And examine the result: it'll be 'false' if no match, or a Match object otherwise. | |
# Match objects exhibit e.g. a 'captures' attribute which is relatively self-evident | |
# when printed via 'p result' or 'puts result.inspect' | |
p result # => false | |
p grok.match("lol a b").captures # => {"FOO:foo1"=>["a"], "FOO:foo2"=>["b"]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment