读取输出日志文件,并使用bash / python脚本打印所有唯一文件路径

时间:2019-04-17 03:33:44

标签: python linux python-3.x bash

从输出日志文件下面,我想使用bash / python脚本打印所有唯一的文件路径(例如/ AWS Cloud / Test / DEMO / Service / DEV)

OS平台:Linux

这是输出日志文件(output.log):

/AWS Cloud/Test/DEMO/Service/DEV:    google.service.instance = https://aoodev.com (ms: azure_mico, cs: docker_telco)
/AWS Cloud/Test/DEMO/Service/QA1:    yahoo.service.instance = aoodit.com (ms: yahoo_mico, cs: yahoo_telco)
/AWS Cloud/Test/Blender/Service/QA1:    google.service.instance = aoodev.com (ms: azure_mico, cs: google_telco)
/AWS Cloud/Test/DEMO/Service/QA1:    yahoo.service.instance = aoodqa.com
/Azure Cloud/Test/DEMO/Service/DEV:    google.service.instance = aoodev.com
/Azure Cloud/Test/DEMO/Service/QA1:    https://yahoo.service.instance = aoodit.com
/Azure Cloud/Test/DEMO/Service/DEV:    google.service.instance = aoodev.com

预期输出:

azure_micro docker_telco / AWS云/测试/演示/服务/ DEV   yahoo_mico yahoo_telco / AWS云/测试/演示/服务/ QA1
  azure_micro google_telco / AWS云/测试/混合器/服务/ QA1
                            / Azure云/测试/演示/服务/ DEV
                            / Azure Cloud / Test / DEMO / Service / DIT

3 个答案:

答案 0 :(得分:1)

您需要regex和python模块re

这应该做到:

paths = [] # Create an empty list of paths
regex = r'^(\/.+\:).*(ms: )(.+), (cs: )(.+)\)$'

with open("logs.txt") as file: # Open your log file
    for line in file:
        if "cs" in line: # If your line has a cs parameter
            result = re.findall(regex, line)[0]
            paths.append(result[2] + " " + result[4] + " " + result[0])
        else:
            paths.append(line.split(":")[0] + ":") # Old way

paths = list(set(paths)) # Convert to set and then back to list to get all unique path only

print(paths)

答案 1 :(得分:-1)

这项工作:

import os
fh = os.open(‘path/to/log’, mode=‘r’)
file_ = fh.readlines()

def parse_paths(file_):
    directories_list = []
    for line in file_:
        path, message = line.split(r‘:\t’)
        directories_list.append(path)
    return directories_list

答案 2 :(得分:-1)

#!/usr/bin/python3

# Open log file as read-only
logFile = open('file.log', 'r')

# To have a nice array with every line of the log file
logLines = logFile.read().split('\n') 

for path in logLines:

    # Divides every line into an array, where line[0] would be the path, and line[1] would have everything after the colon. Then, print it.
    print(path.split(':')[0])