-
Notifications
You must be signed in to change notification settings - Fork 7
/
interleave
executable file
·63 lines (57 loc) · 1.37 KB
/
interleave
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
#
#######################################################################
#
#
# interleave
#
# USAGE: ./interleave [--no-break] file1 file2 [...]
#
# Interleave files to stdout.
#
# Output will be of the following form:
# line 1 of file 1
# line 1 of file 2
# ...
#
# line 2 of file 1
# line 2 of file 2
# ...
#
# At least two files must be given as command-line parameters.
# If the --no-break option is given, then no linebreak is inserted
# between the each set of lines.
#
# TODO
# * Low-memory mode, which doesn't read in the files all at once.
#
#
#######################################################################
import sys, string
args = sys.argv[1:]
no_break = False
if args[0] == "--no-break":
no_break = True
args = args[1:]
# The number of files to be interleaved
cnt = len(args)
assert cnt >= 2
from common.file import myopen
lines = []
for n in args:
# sys.stderr.write("%s\n" % n)
if n == "-":
f = sys.stdin
else:
f = myopen(n)
lines.append(f.readlines())
f.close()
# Make sure all files have the same number of lines
for i in range(1, cnt):
assert len(lines[i]) == len(lines[0])
# Now, actually interleave them
for i1 in range(len(lines[0])):
for i2 in range(cnt):
print string.strip(lines[i2][i1]),
if not no_break: print
if no_break: print