#需求

  • 用户可以模糊查询员工信息

  • 显示匹配了多少条,匹配字符需要高亮度显示


#脚本内容

#!/usr/bin/env python
#_*_ coding:utf-8 _*_
while True:
        info = 'info.txt'
        f = file(info)
        search=raw_input('Please Engter You Search Info: ')
        for line in f.readlines():
        i=line.strip().split()
        q=i[0]
        w=i[1]
        e=i[2]
        r=i[3]
        g=len(search)

        #第1列文件模糊匹配
        if search in q:    
            d=len(q)
            c=q.find(search)
            j=c+g
            if c == 0:
            u=q[:g]
            o=q[g:]
            print  "\033[31m%s\033[0m%s %s %s  %s"  % (u,o,w,e,r)
        elif j == d:
            u=q[:c]
            o=q[c:d]
            print  "%s\033[31m%s\033[0m %s  %s  %s"  % (u,o,w,e,r)
        else:
            u=q[:c]
            o=q[c:j]
            p=q[j:]
            print  "%s\033[31m%s\033[0m%s %s %s  %s"  % (u,o,p,w,e,r)
        
        #第2列文件模糊匹配
        if search in w:   
                d=len(w)         
                c=w.find(search) 
                j=c+g
                if c == 0:
                    u=w[:g]
                    o=w[g:]
                    print  "%s\033[31m %s\033[0m%s  %s  %s"  % (q,u,o,e,r)
                elif j == d:
                    u=w[:c]
                    o=w[c:d]
                    print  "%s %s\033[31m%s\033[0m  %s  %s"  % (q,u,o,e,r)
                else:
                    u=w[:c]
                    o=w[c:j]
                    p=w[j:]
                    print  "%s %s\033[31m%s\033[0m%s  %s  %s"  % (q,u,o,p,e,r)

        #第3列文件模糊匹配
        if search in e:          
                d=len(e)
                c=e.find(search) 
                j=c+g
                if c == 0:
                    u=e[:g]
                    o=e[g:]
                    print  "%s %s\033[31m %s\033[0m%s  %s"  % (q,w,u,o,r)
                elif j == d:
                    u=e[:c]
                    o=e[c:d]
                    print  "%s %s %s\033[31m%s\033[0m  %s"  % (q,w,u,o,r)
                else:
                    u=e[:c]
                    o=e[c:j]
                    p=e[j:]
                    print  "%s %s %s\033[31m%s\033[0m%s  %s"  % (q,w,u,o,p,r)

        #第4列文件模糊匹配
        if search in r:
            d=len(r)
            c=r.find(search) 
            j=c+g
            if c == 0:
                u=r[:g]
                o=r[g:]
                print  "%s %s %s\033[31m %s\033[0m%s"  % (q,w,e,u,o)
            elif j == d:
                u=r[:c]
                o=r[c:d]
                print  "%s %s %s %s\033[31m%s\033[0m"  % (q,w,e,u,o)
            else:
                u=r[:c]
                o=r[c:j]
                p=r[j:]
                print  "%s %s %s %s\033[31m%s\033[0m%s"  % (q,w,e,u,o,p)


#员工信息表展示

[root@localhost opt]# cat info.txt 
wsyht  1315326095 yaowan t891672832@126.com
peter  1823572871 duowan t218460931@163.com
jack   15832908124 tanwan t679312053@139.com
jenkis 17937829012 haowan t357891241@qq.com


#脚本执行展示

wKiom1eCUOngyGRXAADBEY7Zj94295.png-wh_50