Skip to content

Instantly share code, notes, and snippets.

@xai
Created March 13, 2017 16:32
Anonymize logfiles
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 Olaf Lessenich <xai@linux.com>
#
# Distributed under terms of the MIT license.
"""
Anonymize logfiles
"""
import re
import sys
def anon(s):
return ('[ANONYMIZED %s]' % s.upper())
regexes = dict()
regexes['mail'] = re.compile(r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)")
regexes['ip'] = re.compile(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})")
for line in sys.stdin:
for name, regex in regexes.items():
line = regex.sub(anon(name), line)
sys.stdout.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment