-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
706 additions
and
729 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python | ||
|
||
import sys | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser( | ||
description='Split a file into pieces in a round robin fashion. ' | ||
'Covers the missing functionality of GNU split by enabling ' | ||
'multi-line round robin splits.') | ||
|
||
parser.add_argument('output_files', nargs='+', help='output files') | ||
parser.add_argument('--input', default='-', type=str, | ||
help="input file; read stdin when not specified or when '-'") | ||
parser.add_argument('--lines', type=int, default=1, | ||
help='number of lines sent to each output file at each round') | ||
parser.add_argument('--strict', action="store_true", default=False, | ||
help='require that lines split evenly ') | ||
|
||
|
||
args = parser.parse_args() | ||
|
||
input = sys.stdin if (args.input == '-') else open(args.input) | ||
outputs = [open(f, 'w') for f in args.output_files] | ||
n_files = len(outputs) | ||
chunk_size = args.lines | ||
cur_file_idx = -1 | ||
|
||
for i,line in enumerate(input): | ||
if (i % chunk_size == 0): | ||
cur_file_idx = (cur_file_idx + 1) % n_files | ||
outputs[cur_file_idx].write(line) | ||
|
||
for f in outputs: | ||
f.close() | ||
|
||
if args.strict: | ||
if not (i % (args.lines * n_files)) == (args.lines * n_files - 1): | ||
raise ValueError("Input did not divide evenly") | ||
|
||
|
||
|
||
|
||
|
||
|
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
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
Oops, something went wrong.