Skip to content

Instantly share code, notes, and snippets.

@bitprophet
Created December 16, 2011 20:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitprophet/1487768 to your computer and use it in GitHub Desktop.
Save bitprophet/1487768 to your computer and use it in GitHub Desktop.
How to quickly test pure-Ruby Grok pattern strings
# 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