Skip to content

Instantly share code, notes, and snippets.

@spietz-handy
Last active August 29, 2015 14:27
Show Gist options
  • Save spietz-handy/9e04e67f9d4af7ae1ef1 to your computer and use it in GitHub Desktop.
Save spietz-handy/9e04e67f9d4af7ae1ef1 to your computer and use it in GitHub Desktop.
search json logs
import sys
import json
def extract_json(line):
index = line.find('{')
if index == -1:
return None
else:
return json.loads(line[index:])
def get_param(data, key):
keys = key.split(",")
for k in keys:
if k not in data:
print "couldn't find %s in %s" % (k, data)
return None
data = data[k]
return str(data)
if __name__ == '__main__':
iter_args = iter(sys.argv[1:])
search_params = {}
while True:
try:
key = next(iter_args)
val = next(iter_args)
search_params[key] = val
except StopIteration:
break
print search_params
for i in sys.stdin:
data = extract_json(i)
match = True
for key in search_params:
if get_param(data, key) == search_params[key]:
match = True
else:
match = False
break
if match:
print json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment