Python intercepts a day's log, simple operation

Source: Internet
Author: User

1. Explaining the work of the great God, and paying tribute to the "kind of heart to accept the Silence" (http://www.cnblogs.com/drgcaosheng/p/3747820.html)

#!/usr/bin/python#-*- coding:utf-8 -*-import re,sysdef openfile (*args):     print args    try:        f=open (args[0 ], ' R ')     #args [0] represents the file to open, the value of the first parameter under the values         try:             while True:                 lines = f.readlines (100)     #每次读取100行                  if not lines:    #假如没有行了, jump out of the loop                      break                 for line in lines :   #循环每一行                      if (Line.find (args[1]) >=0):   #args [1] represents the text to search for, the value of the second parameter                           Writenewfile (line,args[2])    #假如存在搜索的关键字, and then to Writenewfile function, Args[2] represents the new file          finally:             F.close ()             print  ' * ' *21+ "END" + "*" * 21    #打印结束星号     except IOError:         print args[0]+ " not find!"                 def  Writenewfile (*args):    #定义写入新文件     try:   &nbsP;    newfile=open (args[1], ' a ')    #追加模式打开新文件, does not automatically build 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])     #按点切割文件名 (so the file name should be a bit)     newlogfile=logre[0]+args[1]+ "." +logre[1]    #新文件的名字等于第一个点号前面 + you search character + dot + character after the first dot number    &nbSp;openfile (args[0],args[1],newlogfile)     #调用openfile函数, args[0] and args[1] are Chuli sent over 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 "The actual parameters passed to the Chuli function of args[0] and args[1]


2, operation mode and results.

[email protected] ~]# python cao.py 104.250.3.77-test.log "Dec" ********************start******************** (' 104.250.3.77-test.log ', ' Dec ', ' 104Dec 18.250 ') *********************end*********************



The execution sequence is as follows:
(1), call the Chuli function first, the Python cao.py command after the actual 2 parameters passed to the Chuli function.
(2), executes the Chuli function, prints the starting * number, generates a new file name, calls the OpenFile function, and passes three parameters to the OpenFile function.
(3), execute the OpenFile function, read the file, search for the keyword, after the completion of the search call writenewfile function, write to the new file.
(4), execute the Writenewfile function, open the new file in Append mode, and then write the matching line one line to the new file.



3, little brother Python knowledge, limited logic, *args pass multiple values I got dizzy, so I changed:

#!/usr/bin/python#-*- coding:utf-8 -*- import re,sys def openfile (filename, Searchkey,outfilename):    #定义openfiles函数     try:         f=open (filename, ' R ')      #filename表示要打开的文件, the value under the first parameter          try:                    while True:                 lines = f.readlines (   )   #每次读取100行                  if not lines:     #假如没有行了, jump out of the loop                      break                 for line in lines:    #循环每一行                      if ( Line.find (Searchkey) >=0):      #searchkey表示要搜索的关键字, search matching lines                           writenewfile (line,outfilename)    #假如存在匹配行, and then to Writenewfile function          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 intercepts a day's log, simple operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.