简体   繁体   中英

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

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

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.

Very similar i can achieve it using the notepad++ like mark all lines and then delete bookmark will perform yank operation. Very similar i am trying to implement over in python code which is as following

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 在此处输入图片说明

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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