From 614c48a931a6188222eee798310b0c539c707433 Mon Sep 17 00:00:00 2001 From: TKE Date: Thu, 25 Mar 2021 17:48:49 +0100 Subject: [PATCH] Add kv_parse kv_parse.py parses key=value pairs from logs and returns an dict per line --- kv_parse.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 kv_parse.py diff --git a/kv_parse.py b/kv_parse.py new file mode 100755 index 0000000..a4620f2 --- /dev/null +++ b/kv_parse.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +import re +import json +import sys + +with open(sys.argv[1],'rt') as infile: + data = infile.readlines() + +kv_pat = re.compile('(?P[^= ]+)=(?P"[^"]+"|\S+)') + +log=[] +for line in data: + line_dict={} + line = line.strip() + matches=kv_pat.findall(line) + for match in matches: + line_dict[match[0]] = match[1].strip('"') + log.append(line_dict) + +print(json.dumps(log)) +# with open('log.json','wt') as outfile: +# json.dump(log,outfile) + +