Skip to content

Instantly share code, notes, and snippets.

@alexras
Last active May 10, 2016 21:02
Show Gist options
  • Save alexras/a296927ef857d7b2229f to your computer and use it in GitHub Desktop.
Save alexras/a296927ef857d7b2229f to your computer and use it in GitHub Desktop.
Demultiplex the output of `tail -f *.log`
#!/usr/bin/env python
import argparse
import re
import os
import sys
file_delimiter_regex = re.compile('.*?\=\=\> (.*?) \<\=\=\n')
def tail_demux(filename, output_dir):
if not os.path.exists(filename):
sys.exit('File "%s" not found' % (filename))
os.makedirs(output_dir)
current_file = os.path.join(output_dir, 'preamble')
output_files = {}
with open(filename, 'r') as fp:
for line in fp:
if len(line) == 1:
continue
line_match = file_delimiter_regex.match(line)
if line_match is not None:
current_file = os.path.join(output_dir, os.path.basename(line_match.group(1)))
else:
if current_file not in output_files:
output_files[current_file] = open(current_file, 'w')
output_files[current_file].write(line)
for fp in output_files.values():
fp.flush()
fp.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='demultiplex the output of `tail -f *.log`')
parser.add_argument('filename', help='the file to demux')
parser.add_argument('--output_dir', '-o',
help='the directory in which the demuxed logs should be stored',
default='demuxed')
args = parser.parse_args()
tail_demux(**vars(args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment