Skip to content

Instantly share code, notes, and snippets.

@ezr
Created May 25, 2022 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezr/3f679b735733f6fbe02cbc085eb6c21e to your computer and use it in GitHub Desktop.
Save ezr/3f679b735733f6fbe02cbc085eb6c21e to your computer and use it in GitHub Desktop.
similar to unix comm command
#!/usr/bin/env python3
# Similar to the unix tool comm.
# Does not require input to be in sorted order.
# Defaults to finding the intersection of two lists.
import argparse
def slurp(fname):
f = open(fname)
data = f.read().strip().split("\n")
if args['ignore_case']:
data = map(str.lower, data)
f.close()
return set(data)
def setprint(s):
for x in s:
print(x)
parser = argparse.ArgumentParser(description='compare two lists')
parser.add_argument('-i','--ignore-case', help='case-insensitive comparison', action=argparse.BooleanOptionalAction, required=False, default=False)
# argparse.BooleanOptionalAction requires python version 3.9 or newer
parser.add_argument("-d", "--difference", help='find lines unique to the first file', action=argparse.BooleanOptionalAction, required=False, default=False)
parser.add_argument("files", type=str, nargs=2, help='the files to compare')
args = vars(parser.parse_args())
# TODO support more than 2 files
data0 = slurp(args['files'][0])
data1 = slurp(args['files'][1])
if args['difference']:
print(f"[*] items in {args['files'][0]} not {args['files'][1]}:")
setprint(data0.difference(data1))
else:
print("[*] items common to both files:")
setprint(data0.intersection(data1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment