Skip to content

Instantly share code, notes, and snippets.

@dair-targ
Last active July 4, 2017 13:17
Show Gist options
  • Save dair-targ/fc7478dbffced18af1c3d2cc0440b072 to your computer and use it in GitHub Desktop.
Save dair-targ/fc7478dbffced18af1c3d2cc0440b072 to your computer and use it in GitHub Desktop.
"""
How to use:
cat test.txt | python logg.py
"""
import sys
keywords = ['mmm']
datetime_str = None
# This would be a dictionary
# Keys are dates
# Values are lists of strings which contain one of keywords
result = {}
for line in sys.stdin:
# Skip blank lines
if not line.strip() or line.strip().index('#') == 0:
continue
# Handle lines with "check_time"...
elif 'check_time' in line:
# Assuming format is "check_time<space>:<space>1970/01/01"
# line.split()[0] would be "check_time"
# line.split()[1] would be ":"
# line.split()[2] would be the date we're looking for
datetime_str = line.split()[2]
# This would help with '2017-01-01T00:00:00' format
for c in ['T', '_']:
if c in datetime_str:
datetime_str = datetime_str[:datetime_str.index(c)]
# Handle non-blank lines without "check_time"...
else:
for keyword in keywords:
if keyword in line:
if datetime_str not in result:
result[datetime_str] = [line]
else:
result[datetime_str].append(line)
break
for datetime_str in result:
print '%s -- %d' % (datetime_str, len(result[datetime_str]))
check_time : 2017/06/21 xxx
mmm
hhh
mmm
check_time : 2017/0622
mmm
mmm
kkk
check_time : 2017/06/23
jjj
lll
mmm
mmm
mmm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment