#!/usr/bin/python # grepauth - print lines that mention both Tom and Nat import fileinput import re def build_match_any(words): return re.compile("|".join(words)) def uniq(arr): seen = {} for item in arr: seen[item] = seen.get(item, 0) + 1 return seen.keys() def build_match_all(words): r = re.compile("|".join(words)) c = lambda line: len(uniq(r.findall(line)))>=len(words) return c any = build_match_any(("Tom","Nat")) all = build_match_all(("Tom","Nat")) for line in fileinput.input(): if any.search(line): print "any:", line if all(line): print "all:", line