Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Patterns #30

Open
mthota15 opened this issue Jun 12, 2018 · 4 comments
Open

Multiple Patterns #30

mthota15 opened this issue Jun 12, 2018 · 4 comments

Comments

@mthota15
Copy link

Does pygrok support matching of string against multiple patterns?

Any example on how to do that?

@garyelephant
Copy link
Owner

Please elaborate your requirement, @mthota15

@mthota15
Copy link
Author

I was able to do this way.

from pygrok import Grok
import json
text = [
'37.162.60.195 - - [06/Jun/2018:17:31:29 -0400] "PUT /app/main/posts HTTP/1.0" 200 5055 "http://harris-johnson.com/main/register/" "Mozilla/5.0 (Windows NT 5.0) AppleWebKit/5311 (KHTML, like Gecko) Chrome/13.0.850.0 Safari/5311"',
'::1 - - [26/Dec/2016:16:16:29 +0200] "GET /favicon.ico HTTP/1.1" 404 209',
'[Mon Dec 26 16:22:08 2016] [error] [client 192.168.33.1] File does not exist: /var/www/favicon.ico'
]

patterns = {
'^%{COMBINEDAPACHELOG}$',
'^%{COMMONAPACHELOG}$',
'^%{HTTPD_ERRORLOG}$'
}

def parse_type(text):
for k in patterns:
return Grok(k).match(text)

for i in text:
parsed = parse_type(i)
if parsed:
print(json.dumps(parsed))

please let me know if there is a better way to do it with pygrok.

@tcxdgit
Copy link

tcxdgit commented Jul 8, 2020

+1

@alextremblay
Copy link

grok patterns are a superset of regex patterns. you can use a regex alternator group to match against 2 or more patterns like so:

pattern = r'^(%{COMBINEDAPACHELOG}|%{COMMONAPACHELOG}|%{HTTPD_ERRORLOG})$'
parser = Grok(pattern)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants