Last active
August 29, 2015 14:03
-
-
Save jwalgran/bb87c864676e967d7d12 to your computer and use it in GitHub Desktop.
Linewise merge multiple files
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
#!/usr/bin/env python | |
import argparse | |
from glob import glob | |
def unique_file_list(*glob_strings): | |
file_lists = map(glob, glob_strings) | |
return list(set([item for sublist in file_lists for item in sublist])) | |
def main(): | |
parser = argparse.ArgumentParser(description='Linewise combine multiple files') | |
parser.add_argument('globs', metavar='GLOB', nargs='+', | |
help='Glob matching 1 or more files') | |
parser.add_argument('-d', '--delimiter', help="Delimiter used to join file lines", default='\t') | |
args = parser.parse_args() | |
file_names = unique_file_list(*args.globs) | |
files = map(lambda file_name: open(file_name, 'r'), file_names) | |
more_to_read = True | |
while more_to_read: | |
buffer = [] | |
for f in files: | |
line = f.readline() | |
if line: | |
buffer.append(line.replace('\n', '')) | |
else: | |
more_to_read = False | |
if more_to_read: | |
print(args.delimiter.join(buffer)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment