Skip to content

Instantly share code, notes, and snippets.

@mydreambei-ai
Last active June 7, 2017 08:17
Show Gist options
  • Save mydreambei-ai/4287583b960c48f80c0ae79b62b3376a to your computer and use it in GitHub Desktop.
Save mydreambei-ai/4287583b960c48f80c0ae79b62b3376a to your computer and use it in GitHub Desktop.
Compare files line by line like linux command comm
import argparse
"""
usage:
python compare_files_lines.py -a inter <file1> <file2> == comm -12 <file1> <file2>
python compare_files_lines.py -a union <file1> <file2> == comm <file1> <file2> | sed 's/[\t ]*//g'
python compare_files_lines.py -a diff <file1> <file2> == comm -23 <file1> <file2>
python compare_files_lines.py -a rdiff <file1> <file2> == comm -13 <file1> <file2>
python compare_files_lines.py -a sydiff <file1> <file2> == comm -3 <file1> <file2> | sed 's/[\t ]*//g'
"""
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--action", dest="action", required=True,
choices=["inter", "union", "diff", "sydiff", "rdiff"])
parser.add_argument(
"A", type=argparse.FileType("r"), help="the base file")
parser.add_argument(
"B", type=argparse.FileType("r")
)
args = parser.parse_args()
S = lambda f: set([line.strip() for line in f.readlines()])
O = {"inter": set.__and__, "union": set.__or__, "diff": set.__sub__, "sydiff": set.__xor__,
"rdiff": set.__rsub__}.get(args.action)(S(args.A), S(args.B))
args.A.close()
args.B.close()
for i in O:
print(i, flush=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment