Chinaunix首页 | 论坛 | 博客
  • 博客访问: 165458
  • 博文数量: 37
  • 博客积分: 1132
  • 博客等级: 准尉
  • 技术积分: 380
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-18 16:56
个人简介

吆嘻!

文章分类

全部博文(37)

文章存档

2013年(1)

2012年(19)

2011年(17)

分类: Python/Ruby

2012-05-07 11:10:14

根据关键字过滤存在关键字的行,检索日志
 
 
脚本内容:

点击(此处)折叠或打开

  1. #-*- coding: utf-8 -*-
  2. #/usr/bin/env pyth n
  3. import os
  4. import os.path
  5. import sys

  6. #//
  7. if len(sys.argv) < 3:
  8.     print " No Action specified.\n \
  9.    使用说明: python python_Program.py input.log output.log param1 param2 ... "
  10.     sys.exit()
  11. if sys.argv[1].startswith('--'):
  12.     option = sys.argv[1][2:]
  13.     if option == 'version': #当命令行参数为--version时
  14.         print "Version 1.0"
  15.     elif option == 'help':
  16.         print """\
  17. This program prints files to the standard output.
  18. Any number of files can be specified.
  19. Options include:
  20.     -- version : Prints the version number
  21.     -- help : Display this help """
  22.     else:
  23.         print 'Unknown option'
  24.     sys.exit()
  25. else:
  26.     keyword = sys.argv[3:]
  27.     print '输入的关键字为:-->',keyword,';''关键字数量为:-->',len(keyword)

  28. #//
  29. the_input_file_path = sys.argv[1]

  30. if not os.path.exists(the_input_file_path):
  31.     print '文件不存在,pass'
  32.     sys.exit(1)

  33. print '要检查的log文件是', the_input_file_path

  34. #//
  35. the_input_file = open(the_input_file_path, 'r')

  36. the_input_line_s = the_input_file.readlines()

  37. #//
  38. the_output_file_path = sys.argv[2]

  39. if os.path.exists(the_output_file_path):
  40.     print '文件已存在,不进行创建'
  41. else:
  42.     os.system('{} {}'.format('/bin/touch', the_output_file_path)) #保存出来的日志
  43.     print "创建文件,并打印文件路径"

  44. the_output_file = open(the_output_file_path, 'w')

  45. #//
  46. def write_lines_to_output_file(output_file, line_s):
  47.     try:
  48.         output_file.writelines(line_s)
  49.     finally:
  50.         output_file.close()

  51. #//
  52. the_keyword_s = sys.argv[3:]

  53. for the_keyword in the_keyword_s:

  54.     the_matched_input_line_s = []

  55.     print('关键字: {}'.format(the_keyword))

  56.     for the_input_line in the_input_line_s:
  57.         if the_keyword in the_input_line:
  58.             print('{} {}'.format(repr(the_input_line), '匹配'))
  59.             the_matched_input_line_s.append(the_input_line)
  60.         else:
  61.             print('{} {}'.format(repr(the_input_line), '不匹配'))

  62.     if not the_matched_input_line_s:
  63.         break
  64.     else:
  65.         the_input_line_s = the_matched_input_line_s

  66. if len(the_matched_input_line_s) > 0:
  67.     print "保存匹配行到:{}".format(the_output_file_path)
  68.     write_lines_to_output_file(the_output_file, the_matched_input_line_s)

  69. print 'Done'

 

 

测试:


 

点击(此处)折叠或打开

  1. [root@Tnode01 ~]# cat 5746.log
  2. 1a
  3. 2b
  4. 3c
  5. 4d
  6. 5e
  7. 6f
  8. 8g6f1a



 

点击(此处)折叠或打开

  1. [root@Tnode01 ~]# python a1.py 5746.log output.1txt 6f
  2. 输入的关键字为:--> ['6f'] ;关键字数量为:--> 1
  3. 要检查的log文件是 5746.log
  4. 文件已存在,不进行创建
  5. ['6f']
  6. 关键字: 6f
  7. '6f\n' 匹配
  8. '8g6f1a\n' 匹配
  9. 'output.1txt'
  10. Done
  11. [root@Tnode01 ~]# cat output.1txt
  12. 6f
  13. 8g6f1a


 

点击(此处)折叠或打开

  1. 给两个关键字6f 1a输出结果为:
  2. [root@Tnode01 ~]# python a1.py 5746.log output.1txt 6f 1a
  3. 输入的关键字为:--> ['6f', '1a'] ;关键字数量为:--> 2
  4. 要检查的log文件是 5746.log
  5. 文件已存在,不进行创建
  6. ['6f', '1a']
  7. 关键字: 6f
  8. '6f\n' 匹配
  9. '8g6f1a\n' 匹配
  10. 关键字: 1a
  11. '8g6f1a\n' 匹配
  12. 'output.1txt'
  13. Done
  14. [root@Tnode01 ~]# 8g6f1a
  15. [root@Tnode01 ~]# cat output.1txt
  16. 8g6f1a



 

阅读(1132) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~