forked from mjpost/bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpaste
executable file
·54 lines (42 loc) · 1.74 KB
/
unpaste
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
#!/usr/bin/env python3
# *-* encoding: utf-8 *-*
"""
Takes a tab-delimited stream on STDIN and writes the columns in turn to the files specified as
arguments (i.e., the opposite of UNIX `paste`). If there are more fields than files, the last file
will get all remaining fields. If there are more files than fields, files beyond the last field will
get blank lines.
paste file1 file2 [...] | ./unpaste file1.copy file2.copy [...]
Author: Matt Post <[email protected]>
"""
import sys
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--append', '-a', action='store_true', default=False,
help='Append to files instead of overwriting.')
parser.add_argument('--tee', '-t', action='store_true', default=False,
help='Also echo to STDOUT')
parser.add_argument('--delimiter', '-d', type=str, default='\t',
help='Delimiter to use')
parser.add_argument('files', nargs='+',
help='File(s) to write to')
args = parser.parse_args()
def _writeto(filename, append=False):
mode = 'at' if append else 'wt'
if filename.endswith('.gz'):
import gzip
return gzip.open(filename, mode)
else:
return open(filename, mode)
fhs = list(map(lambda x: _writeto(x, args.append), args.files))
for line in sys.stdin:
line = line.rstrip('\n')
fields = line.split(args.delimiter, maxsplit=len(fhs)-1)
for i, field in enumerate(fields):
print(field, file=fhs[i])
for i in range(len(fields), len(fhs)):
print(file=fhs[i])
if args.tee:
print(line)
for fh in fhs:
fh.close()