- 论坛徽章:
- 4
|
本帖最后由 mswsg 于 2016-05-03 21:22 编辑
将代码保存为 filter_by_month.py,
python filter_by_month.py --input your_input_file -m 3 --output your_out_file
-m 3 几个月之前,比如3个月之前即03/Nov/2015之前的日志- $ python data_time.py --input 1.txt -m 6 --output 1.out.txt
- You will find after this time 03/Nov/2015 21:17:40 line!
- $ head 1.out.txt
- 63.54.78.89 UNKNOWN w0500465 [03/Sep/2016:16:16:26 +0800] 275 119098 0.019
- 192.168.100.126 UNKNOWN dingjunhui [15/Mar/2016:09:15:35 +0800] 426 1109168 3.215
复制代码- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import sys
- import getopt
- from datetime import datetime
- import time
- from dateutil.relativedelta import relativedelta
- import re
- __author__ = 'shengwei ma'
- __author_email__ = 'shengweima@icloud.com'
- input_file = ""
- output_file = ""
- a = ""
- try:
- opts, args = getopt.getopt(sys.argv[1:], "hm:", ["input=", "output="])
- except getopt.GetoptError as err:
- print(str(err))
- for op, value in opts:
- if op == "--input":
- input_file = value
- elif op == "-m":
- a = int(value)
- elif op == "--output":
- output_file = open(value, 'w')
- elif op == "-h":
- print("python filter_by_month.py --input your_input_file -m 3 --output your_out_file")
- sys.exit()
- months = datetime.now() + relativedelta(months=-a)
- month = months.strftime("%d/%b/%Y %H:%M:%S")
- print 'You will find after this time %s line!' % month
- with open(input_file, 'r') as f:
- for num, line in enumerate(f):
- if num % 2 == 0:
- new_line = line.strip().split()
- date = new_line[3].lstrip('[').split(':')
- new_date = date[0] + ' ' + date[1] + ':' + date[2] + ':' + date[3]
- timeArray_b = time.strptime(new_date, "%d/%b/%Y %H:%M:%S")
- timeArray_a = time.strptime(month, "%d/%b/%Y %H:%M:%S")
- if timeArray_b > timeArray_a:
- if new_line[0].startswith('192') and re.findall(r'[^w]', new_line[2]):
- output_file.write(line)
- elif new_line[2].startswith('w') and re.findall(r'[^192]', new_line[0]):
- output_file.write(line)
复制代码 |
|