简体   繁体   English

在python中搜索字符串匹配的一部分

[英]Search part of string matching in python

I have two files one is "error_dict" file and other is "jbosslogfile" error dict file contains known error messages in each line like error_dict 我有两个文件,一个是“ error_dict”文件,另一个是“ jbosslogfile”错误dict文件在每一行中都包含已知的错误消息,例如error_dict

"0500 ERROR [org.picketlink.identity.federation]"
"-0500 ERROR [com.gravitant.cloud.appsportfolio.jsf.architecture.beans.StorageAccountBean]"
"Invalid Context Code - APP"

JbossLogFile JbossLogFile

2016-06-03 00:19:52,612 -0500 ERROR [org.jboss.as.ejb3.invocation] (EJB default - 3) JBAS014134: EJB Invocation failed on component GraBmsDataLoaderEjbIfc for method public abstract void com.gravitant.bms.common.dataload.GraBmsDataLoaderEjbIfc.loadVMTemplates(java.lang.String,java.lang.String,java.lang.String): javax.ejb.EJBException: com.gravitant.bts.bms.exception.GraException: Display Message:Transaction commit failed - 
2016-06-03 00:20:10,809 -0500 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (EJB default - 4) Duplicate entry '9db562c525e942698c605df2f0c9b26a__FreeBSD10_3-2016-06-02::unreco' for key 'UQ_graresourcetemplate$tmpId_prv_usrGrpId'
2016-06-03 00:20:10,824 -0500 ERROR [com.gravitant.bts.bms.transaction.BTSTransaction] (EJB default - 4) BmsBaseTransaction:Commit()- Transaction commit failed 
2016-06-03 00:20:11,001 -0500 ERROR [org.jboss.as.ejb3.invocation] (EJB default - 4) JBAS014134: EJB Invocation failed on component GraBmsDataLoaderEjbIfc for method public abstract void com.gravitant.bms.common.dataload.GraBmsDataLoaderEjbIfc.loadVMTemplates(java.lang.String,java.lang.String,java.lang.String): javax.ejb.EJBException: com.gravitant.bts.bms.exception.GraException: Display Message:Transaction commit failed - 
2016-06-03 00:31:56,730 -0500 ERROR [org.picketlink.identity.federation] (http-/10.200.212.143:8081-10) PLFED000263: Service Provider could not handle the request.: java.lang.IllegalArgumentException: PLFED000132: No assertions in reply from IDP
2016-06-03 00:52:01,379 -0500 ERROR [org.picketlink.identity.federation] (http-/10.200.212.143:8081-1) PLFED000263: Service Provider could not handle the request.: java.lang.IllegalArgumentException: PLFED000132: No assertions in reply from IDP
2016-06-03 01:11:49,938 -0500 ERROR [org.picketlink.identity.federation] (http-/10.200.212.143:8081-10) PLFED000263: Service Provider could not handle the request.: java.lang.IllegalArgumentException: PLFED000132: No assertions in reply from IDP
2016-06-03 01:41:59,942 -0500 ERROR [org.picketlink.identity.federation] (http-/10.200.212.143:8081-1) PLFED000263: Service Provider could not handle the request.: java.lang.IllegalArgumentException: PLFED000132: No assertions in reply from IDP
2016-06-03 02:02:04,783 -0500 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/].[Faces Servlet]] (http-/10.200.212.143:8081-1) JBWEB000236: Servlet.service() for servlet Faces Servlet threw exception: javax.faces.application.ViewExpiredException: viewId:/appsportfolio-main.jsf - View /appsportfolio-main.jsf could not be restored.
2016-06-03 02:11:57,211 -0500 ERROR [org.picketlink.identity.federation] (http-/10.200.212.143:8081-1) PLFED000263: Service Provider could not handle the request.: java.lang.IllegalArgumentException: PLFED000132: No assertions in reply from IDP
2016-06-03 02:22:02,739 -0500 ERROR [org.picketlink.identity.federation] (http-/10.200.212.143:8081-10) PLFED000263: Service Provider could not handle the request.: java.lang.IllegalArgumentException: PLFED000132: No assertions in reply from IDP
201

What i am trying to do is very simple i am using this error_dict file each known error to match against my jboss log file in which case if i found part of string from jboss log file print that line on screen and then yank that line from jboss log file. 我想做的非常简单,我使用这个error_dict文件将每个已知的错误与我的jboss日志文件进行匹配,在这种情况下,如果我从jboss日志文件中找到了字符串的一部分,则在屏幕上打印该行,然后从jboss中提取该行日志文件。

Very similar i can achieve it using the notepad++ like mark all lines and then delete bookmark will perform yank operation. 非常相似,我可以使用notepad ++来实现,例如标记所有行,然后删除书签将执行yank操作。 Very similar i am trying to implement over in python code which is as following 非常相似,我正在尝试在python代码中实现如下

def MatchCountYankerrors():
    ferrorrepo = errorrepopath
    conclufile = os.path.join(Decompressfilepath,(appservername+'conclusion'))
    ferrorfile = open(ferrorrepo)
    confile = open(conclufile)
    output = []

    for errlines in ferrorfile: #Pick each line from error_dict
        c = 0
        for eachconline in confile:#pick each line from Jboss log
            #if re.search(errlines,eachconline,re.M|re.I):
            newerrliens = errlines.strip().split() #error_dict file each line strip and spilit
            neweachconline = eachconline.strip().split() #Jbosslog file each line strip and spliting
            if newerrliens in neweachconline:
                print neweachconline

Objective: How to perform following operation using python program 目标:如何使用python程序执行以下操作 在此处输入图片说明

Here is code which loop through my error repo files and in final error log file if there is match with line it will yank(delete) that line from log file and before performing this operation it will create backup file. 这是遍历我的错误回购文件和最终错误日志文件中的代码,如果与该行匹配,它将从日志文件中删除(删除)该行,并且在执行此操作之前,它将创建备份文件。

#This function will be matching error lines from error repo files and if matched it will yank those lines from the files.
def MatchandYankerrors():
    ferrorrepo = errorrepopath
    conclufile = os.path.join(Decompressfilepath,(appservername+'conclusion'))
    ferrorfile = open(ferrorrepo)
    output = []

    for errlines in ferrorfile: #Pick each line from error_dict
        c = 0
        newerrliens = errlines.strip()  # error_dict file each line strip and spilit
        #confile = open(conclufile,"r+")#This will keep opening file every time when we need new error to search.
        #confilelines = confile.readlines() #This will read all lines from file.
        #confile.seek(0)
        i=0
        for line in fileinput.input(conclufile,inplace=1,backup='.orig'):
            line = line.strip()
            if newerrliens in line:
                pass
            else:
                print line
        fileinput.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM