Skip to content

Instantly share code, notes, and snippets.

@seahrh
Created July 30, 2020 11:29
Show Gist options
  • Save seahrh/fb395bbe301d7c6348fd3213a28fbf27 to your computer and use it in GitHub Desktop.
Save seahrh/fb395bbe301d7c6348fd3213a28fbf27 to your computer and use it in GitHub Desktop.
Python3 regex match timestamp pattern
import re
from typing import List, Set
filename = ""
out_filename = ""
records: List[str] = []
ts_pattern = re.compile(r"^.+(\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2})")
seen: Set[str] = set()
with open(filename) as lines:
for line in lines:
m = ts_pattern.match(line)
if m is not None:
ts: str = m.group(1)
if ts in seen:
records.append(ts)
else:
seen.add(ts)
with open(out_filename, mode='wt', encoding='utf-8') as out:
out.write('\n'.join(records))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment