用户工具


使用方式
python fileDiff.py -s , -iq 'a|b&c-d^e'

参数
   -s 指定换行字符
   -i 忽略指定字符,后面跟一个字符
   -iq 忽略单引号,双引号,后面没有参数


操作符
    【|】: 取交集
    【&】: 取并集
    【-】: 相当于removeAll
    【^】: 并集减去交集 
#!/usr/bin/env python
##################################################
#                                                #
#  desc:    find out diff record between to file #
#  author:  fangqiang                            #
#  date:    2014-01-02                           #
#                                                #
##################################################
from argparse import ArgumentParser

import re,sys

SPLITER = False
IGNORE_QUOT = False
IGNORE = False

def formatLine(line):
    if IGNORE:
        for x in IGNORE:
            line = line.replace(x, "")

    if IGNORE_QUOT:
        line = line.replace("'","")
        line = line.replace('"',"")

    if SPLITER:
        return set(l.strip() for l in line.split(SPLITER) if len(l.strip()) != 0)

    return set([line])

""" file to set """
def f(file):
    res = set()
    lines = open(file).readlines()
    for line in lines:
        if len(line.strip()) != 0:
            res = res | formatLine(line.strip())

    return res

if __name__ == "__main__":

    p = ArgumentParser(usage='find different line between two file', description='fill diff')
    p.add_argument('-s') 
    p.add_argument('-iq', action='append_const', const='') 
    p.add_argument('-i') 
    p.add_argument('eval') 

    args = p.parse_args()

    if args.s:
        SPLITER = args.s

    if args.iq:
        IGNORE_QUOT = True

    if args.i:
        IGNORE = set(l.strip() for l in args.i.split(",") if len(l.strip()) != 0)

    evalString = args.eval
    varSet = set(re.split(r'[\(\)\|\&\-\^]+', evalString))
    for var in varSet:
        if len(var.strip()) > 0:
            evalString = evalString.replace(var.strip(), "f('"+ var.strip() +"')")

    """ f1 | f2 -> f('f1') | file('f2') """
    res = eval(evalString)
    
    for line in sorted(res):
        print line