Created
February 13, 2013 22:16
-
-
Save saml/4948867 to your computer and use it in GitHub Desktop.
split stdin into different files at certain lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import re | |
pattern = re.compile(r'%s' % sys.argv[1]) | |
filename_prefix = sys.argv[2] | |
filename_postfix = sys.argv[3] | |
curr_file = None | |
while True: | |
line = sys.stdin.readline() | |
if not line: | |
break | |
m = pattern.match(line) | |
if m: | |
if curr_file: | |
curr_file.close() | |
curr_file = None | |
curr_file = open(filename_prefix + line.strip() + filename_postfix, 'w') | |
if curr_file: | |
curr_file.write(line) | |
if curr_file: | |
curr_file.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment