"""Print selected columns of rows matching a simple filter, for manual inspection. Usage: python3 -I off_peek.py [filtercol=regex] """ import gzip, sys, re path, skip, count, cols = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), sys.argv[4].split(',') flt = None if len(sys.argv) > 5: fc, fr = sys.argv[5].split('=', 1) flt = (fc, re.compile(fr)) with gzip.open(path, 'rb') as f: header = f.readline().decode('utf-8').rstrip('\n').split('\t') H = {c: i for i, c in enumerate(header)} i = shown = 0 for raw in f: i += 1 if i <= skip: continue p = raw.decode('utf-8', 'replace').rstrip('\n').split('\t') if len(p) != len(header): continue if flt and not flt[1].search(p[H[flt[0]]]): continue print(' | '.join(f'{c}={p[H[c]]!r}' for c in cols)) shown += 1 if shown >= count: break