allLines = (x.strip().split(' ') for x in sys.stdin)
groups = itertools.groupby(allLines, lambda x: x[1])
for k, lines in groups:
    firstLine = next(iter(g))
Merging 2 very large text files, update each line, without using memory