一个Python3脚本,可以实现从某一目录下的所有文本文件例如日志中,根据关键字提取指定行,用法为python3 脚本名称 检索路径 关键字(多个关键字用逗号分隔) 关键字向上取行数 关键字向下取行数,例如:python3 findmsg.py /tmp/logs/ warn,error 5 10

import os,linecache,sys

def setcolor(text,keyword):
    perfix = '\033[1;35m'
    suffix = '\033[0m'
    keywordlist = keyword.split(',')
    for i in keywordlist:
        text = text.replace(i,perfix+i+suffix)
    return text

def findmsg_h(filedir,msg,unum,dnum):
    worklist = os.listdir(filedir)
    msglist = msg.split(',')
    for i in range(0,len(worklist)):
        path = os.path.join(filedir,worklist[i])
        if os.path.isfile(path):
            print('当前查找的文件为' +path)
            msgfile = open(path)
            for nums,value in enumerate(msgfile):
                if all(msgtotal in value for msgtotal in msglist):
                    print('当前匹配的关键字为'+str(msglist))
                    for point in range(nums-int(unum)+1,nums+int(dnum)+1+1,):
                        linetext = linecache.getline(path,point).strip()
                        print(point,setcolor(linetext,msg))
                    print('=================================================')
            msgfile.close()
    print('匹配完成')
    return

findmsg_h(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4])
分类: Python3

9 条评论

judibola828bet · 2025-12-19 20:06

Oi, judibola828bet, eh? A bit of a gamble, innit? But hey, if you fancy a punt, give it a go! Just remember, gamble responsibly! judibola828bet

ph33comapplogin · 2025-12-24 10:47

Needed to log in to ph33comapplogin the other day and it was actually pretty smooth. App worked well, no glitches, easy to get around. If you’re having trouble finding the login, here’s a direct link: ph33comapplogin Might save you some time!

ph22login · 2026-01-04 07:22

That’s a solid analysis – understanding those quick deposit options (like GCash!) is key for seamless betting. I’ve been checking out ph22 login games and the instant verification is a huge plus – no waiting around to play! Definitely streamlining the experience.

phtaya06 · 2026-01-14 22:02

phtaya06 https://www.phtaya06y.com

playpal77 · 2026-01-14 22:21

playpal77 https://www.playpal77sy.org

okebet3 · 2026-01-14 22:37

okebet3 https://www.okebet3u.org

fg777link · 2026-01-15 00:01

fg777link https://www.befg777link.com

okbet15 · 2026-01-15 02:15

okbet15 https://www.okbet15.org

ph444 · 2026-01-18 07:19

Interesting read! Seeing platforms like ph444 app focus on probability & RTP is smart. It’s good to see data-driven approaches in Philippine gaming – makes things a bit more strategic, don’t you think? 🤔

回复 okbet15 取消回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注