Skip to content

Instantly share code, notes, and snippets.

@saml
Created February 13, 2013 22:16
Show Gist options
  • Save saml/4948867 to your computer and use it in GitHub Desktop.
Save saml/4948867 to your computer and use it in GitHub Desktop.
split stdin into different files at certain lines
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