1. 程式人生 > >python 截取某一天的日誌,簡單操作

python 截取某一天的日誌,簡單操作

python 截取某一天的日誌簡單操 截取日誌 一天的日誌

1、解釋大神之作,致敬“種心收默”(http://www.cnblogs.com/drgcaosheng/p/3747820.html)

#!/usr/bin/python
#-*- coding:utf-8 -*-

import re,sys
def openfile(*args):
    print args
    try:
        f=open(args[0],'r')   #args[0]表示要打開的文件,第一個參數下標的值
        try:
            while True:
                lines = f.readlines(100)   #每次讀取100行
                if not lines:   #假如沒有行了,則跳出整個循環
                    break
                for line in lines:  #循環每一行
                    if(line.find(args[1])>=0):  #args[1]表示要搜索的文字,第二個參數下標的值
                        writenewfile(line,args[2])  #假如存在搜索的關鍵字,然後傳到writenewfile函數,args[2]表示新文件
        finally:
            f.close()
            print '*'*21+"END"+"*"*21   #打印結束星號
    except IOError:
        print args[0]+" not find!"

                
def writenewfile(*args):   #定義寫入新文件
    try:
        newfile=open(args[1],'a')  #追加模式打開新文件,沒有會自動建一個
        try:
            newfile.write(args[0])    #一行一行的寫到新文件中
        finally:
            newfile.close()
    except IOError:
        print args[1]+"not find!!"   #如果錯誤就提示新文件找不到

                
def chuli(*args):
    print '*'*20+"START"+"*"*20    #打印開始星號
    logre=re.split('\.',args[0])   #按點切割文件名(所以文件名要有點號)
    newlogfile=logre[0]+args[1]+"."+logre[1]   #新文件的名字等於第一個點號前面+你搜索字符+點號+第一個點號後面的字符
    openfile(args[0],args[1],newlogfile)   #調用openfile函數,args[0]和args[1]是chuli傳過來sys.argv[1],sys.argv[2]。

        
if __name__=='__main__':
    chuli(sys.argv[1],sys.argv[2])   #獲取python cao.py 104.250.149.146-test.log "Dec 18"的實際參數傳給chuli函數的args[0]和args[1]


2、運行方式和結果。

[root@localhost ~]# python cao.py 104.250.3.77-test.log "Dec 18"
********************START********************
('104.250.3.77-test.log', 'Dec 18', '104Dec 18.250')
*********************END*********************



執行順序如下:
(1)、先調用chuli函數,將python cao.py命令後實際的2個參數傳遞給chuli函數。

(2)、執行chuli函數,打印開始的*號,生成新文件名,並調用openfile函數,並且將三個參數傳給了openfile函數。
(3)、執行openfile函數,讀取文件,搜索關鍵字,搜索完成後調用writenewfile函數,寫入新文件中。
(4)、執行writenewfile函數,以追加方式打開新文件,然後把匹配的行一行一行寫入到新文件中。



3、小弟python知識、邏輯有限,*args傳多個值把我搞暈了,所以我改下:

#!/usr/bin/python
#-*- coding:utf-8 -*-
 
import re,sys
 
def openfile(filename,searchkey,outfilename):   #定義openfiles函數
    try:
        f=open(filename,'r')    #filename表示要打開的文件,第一個參數下標的值
        try:       
            while True:
                lines = f.readlines(100)    #每次讀取100行
                if not lines:    #假如沒有行了,則跳出整個循環
                    break
                for line in lines:   #循環每一行
                    if(line.find(searchkey)>=0):     #searchkey表示要搜索的關鍵字,搜索匹配行
                        writenewfile(line,outfilename)  #假如存在匹配行,然後傳到writenewfile函數
        finally:
            f.close()
            print '*'*21+"END"+"*"*21
    except IOError:
        print filename+" not find!"
 
 
 
def writenewfile(line,name):    #定義writenewfile函數
    try:
        newfile=open(name,'a')   #打開文件args[1]
        try:
            newfile.write(line)
        finally:
            newfile.close()
 
    except IOError:
        print name+"not find!!"

        
def chuli(search,outfile):
    print '*'*20+"START"+"*"*20
    logre=re.split('\.',search)
    newlogfile=logre[0]+outfile+"."+logre[1]
    openfile(search,outfile,newlogfile)
 
 
if __name__=='__main__':
    chuli(sys.argv[1],sys.argv[2])


python 截取某一天的日誌,簡單操作