日志文件除重并生成新的文件

1、log文件格式:
======================
Thread T9 (GTest) created by T0 here:
    #0 0xb7237dcc in pthread_create (/usr/lib/i386-linux-gnu/libasan.so.2+0x2fdcc)
    #1 0xacaede0d  (libGTestServer.so+0x2ce0d)

SUMMARY: AddressSanitizer: alloc-dealloc-mismatch ??:0 operator delete(void*)
==16061==HINT: if you don't care about these warnings you may set ASAN_OPTIONS=alloc_dealloc_mismatch=0
==16061==ABORTING
======================
2、脚本
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 文件名:ReplaceTXT.py
import os
import xlwt
#1.创建一个excell文件
workbook = xlwt.Workbook() #注意Workbook的开头W要大写
#2.创建一个sheet
sheet1 = workbook.add_sheet('sheet1',cell_overwrite_ok=True)
#文件名
file_x = open('D:\\worksoft\\ubuntu32_asan_asan_4.1.7.165\\nohup.out'.decode('utf-8').encode('cp936'))#file_y = open('E:\\model_output_log\\y\\_iter_36000_yhand_output.txt')
dir = 'C:\\work\\1220\\y\\'
# 临时变量,sheet的行
temp = 0
def solution( re_str,test_str ):
     # 如果两个字符串相等 就返回True
     if re_str == test_str :
         return True
     # 标记第一个字母
     r = re_str[0] if re_str != '' else ''
     t = test_str[0] if test_str !='' else ''
     # r 不是? 也 不是* 的情况
     if r != '?' and r != '*' :
         if r != t :     # 如果不想相等就返回False
             return False
         else :      # 相等 就 删掉第一个单词 递归
             re_str,test_str = re_str[1:],test_str[1:]
             return solution( re_str,test_str )
     # 如果r是? 相当于匹配一个字符 都删掉一个字符 然后 递归
     if r == '?' :
         re_str, test_str = re_str[1:], test_str[1:]
         return solution(re_str, test_str)
     # 如果r是*  re 是n个*  则返回True
     if r == '*' and re_str.strip('*') == '' :
        return True
     # 否则 就是包含* ,*匹配0个字符或多个字符,所以我们返回 递归 0个匹配 与 1个匹配 的逻辑或
     return solution(re_str[1:], test_str) or solution(re_str, test_str[1:])
def text_create(name, msg):
    desktop_path = 'D:\\log\\'
    full_path = desktop_path + name + '.txt'
    file = open(full_path, 'x')
    file.write(msg)
    file.close()
    print('Done')
#新建文件目录
#参数path:目录文件名
#成功返回1
#失败返回0
def createfile(path):
    # 去除首位空格
    path = path.strip()
    # 去除尾部 \ 符号
    path = path.rstrip("\\")
    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists = os.path.exists(path)
    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path)
        print path + ' 创建成功'
        return True
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print path + ' 目录已存在'
        return False

temp = 0
list1 = []
file_name = "bug_ubuntu_asan_"
file_num = 1
desktop_path = 'D:\\log\\'
for line in file_x.readlines():
    full_path = desktop_path + file_name + str(file_num) + '.txt'
    file = open(full_path, 'a')
    # line = line.strip('\n')
    re_str = "==============================*"
    res = solution(re_str, line)
    remark = False
    if res:
        remark = True
        if temp == 1:
            temp = 0
            file_num += 1
            # list1.append(line)
        temp += 1
    if remark:
        continue
    file.write(line)
    re_str1 = "SUMMARY*"
    res1 = solution(re_str1, line)
    if res1:
        list1.append(line)
file.close()
list1 = list(set(list1))
print "list===", len(list1)
temp_list = []

for i in list1:
    temp1 = 0
    for root, save_file, files in os.walk(desktop_path):
        for file in files:
            file_txt = open(root+file)
            for line in file_txt.readlines():
                if temp1 == 1:
                    pass
                else:
                    res = solution(i, line)
                    if res:
                        # import shutil
                        # temp_file_txt = open(root+"temp"+"\\\\"+file)
                        # shutil.copyfile(root+file, temp_file_txt)
                        print "not same====", line
                        print "file====", file
                        temp_list.append(file)
                        temp1 += 1
import shutil
for i in range(len(temp_list)):
    file_name = desktop_path+"\\temp\\"+temp_list[i]
    createfile(desktop_path+"\\temp\\")
    shutil.copyfile(desktop_path + temp_list[i], file_name)
    file_txt = open(file_name,"r")
    lines = file_txt.readlines()
    file_txt.close()
    new_lines = [" "]
    new_lines[0] = "[UBUNTU_MONKEY_ASAN]" + list1[i] + "\n"
    for j in range(len(lines)):
        new_lines.append(lines[j])
    file_txt = open(file_name, "w")
    for j in new_lines:
        file_txt.write(j)
    file_txt.close()
    print "lines===", new_lines

猜你喜欢

转载自blog.csdn.net/qq_34568522/article/details/92365352