def subset(args):
    """
    %prog subset file1.txt(pivotfile) file2.txt ..

    subset tabular-like file1 based on common column with file 2.
    Normally file1 should have unique row entries.
    If more than one file2 are provided, they must have same column separators.
    Multiple file2's will be concatenated in the output.

    --column specifies the column index (0-based) to pivot on.
      Use comma to separate multiple values if the pivot column is different
      in each file. Maintain the order in the first file.
    --sep specifies the column separators, default to tab.
      Use comma to separate multiple values if the column separator is different
      in each file.
    """

    p = OptionParser(subset.__doc__)
    p.add_option("--column", default="0",
                 help="0-based column id, multiple values allowed [default: %default]")
    p.set_sep(multiple=True)
    p.add_option("--pivot", default=1, type="int",
                 help="1 for using order in file1, 2 for using order in \
                    file2 [default: %default]")
    p.set_outfile()

    opts, args = p.parse_args(args)
    nargs = len(args)

    if len(args) < 2:
        sys.exit(not p.print_help())

    c = opts.column
    if "," in c:
        cc = [int(x) for x in c.split(",")]
        assert len(set(cc[1:])) == 1, \
            "Multiple file2's must have same column index."
        cc = cc[0:2]
    else:
        cc = [int(c)] * 2

    s = opts.sep
    if "," in s:
        ss = [x for x in s.split(",")]
        assert len(set(cc[1:])) == 1, \
            "Multiple file2's must have same column separator."
        ss = ss[0:2]
    else:
        ss = [s] * 2

    if nargs > 2:
        file2 = FileMerger(args[1:], outfile="concatenatedFile2").merge()
    else:
        file2 = args[1]
    newargs = [args[0], file2]

    files = [DictFile(f, keypos=c, valuepos=None, delimiter=s) \
                        for f, c, s in zip(newargs, cc, ss)]

    pivot = 0 if opts.pivot==1 else 1
    fp = open(newargs[pivot])
    fw = must_open(opts.outfile, "w")

    for row in fp:
        row = row.rstrip()
        atoms = row.split(ss[pivot])
        key = atoms[cc[pivot]]
        d = files[1-pivot]
        if key in d:
            print(ss[0].join(files[0][key]), file=fw)

    if nargs > 2:
        FileShredder([file2])
%prog subset file1.txt(pivotfile) file2.txt ..

    subset tabular-like file1 based on common column with file 2.
    Normally file1 should have unique row entries.
    If more than one file2 are provided, they must have same column separators.
    Multiple file2's will be concatenated in the output.

    --column specifies the column index (0-based) to pivot on.
      Use comma to separate multiple values if the pivot column is different
      in each file. Maintain the order in the first file.
    --sep specifies the column separators, default to tab.
      Use comma to separate multiple values if the column separator is different
      in each file.