简体   繁体   English

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

[英]read output log file, and print all unique file paths using bash/python script

From below output log file, I want to print all unique file paths ( eg /AWS Cloud/Test/DEMO/Service/DEV ) using bash/python script从下面的输出日志文件中,我想使用 bash/python 脚本打印所有唯一的文件路径(例如 /AWS Cloud/Test/DEMO/Service/DEV)

OS Platform: Linux操作系统平台:Linux

Here's the Output log file (output.log):这是输出日志文件 (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

Expected Output:预期输出:

azure_micro docker_telco /AWS Cloud/Test/DEMO/Service/DEV yahoo_mico yahoo_telco /AWS Cloud/Test/DEMO/Service/QA1 azure_micro docker_telco /AWS Cloud/Test/DEMO/Service/DEV yahoo_mico yahoo_telco /AWS Cloud/Test/DEMO/Service/QA1
azure_micro google_telco /AWS Cloud/Test/Blender/Service/QA1 azure_micro google_telco /AWS Cloud/Test/Blender/Service/QA1
/Azure Cloud/Test/DEMO/Service/DEV /Azure 云/测试/演示/服务/开发
/Azure Cloud/Test/DEMO/Service/DIT /Azure 云/测试/演示/服务/DIT

You need regex and python module re你需要正则表达式和python模块re

This should do it:这应该这样做:

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)

Does this work:这是否有效:

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
#!/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]) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 读取文件夹中的所有文件,并为每个文件创建唯一的输出 - Python - Read all files in a folder and create unique output for each file 使用 bash 脚本从日志文件中提取所有 JavaScript 文件名 - Extracting all JavaScript filenames from a log file using bash script 在 Python 中打印 bash 脚本输出 - Print bash script output in Python 使用bash脚本从stdin读取文件并将读取的行传递给python脚本的最佳方法是什么 - What is the best way to read a file from stdin using bash script and pass the read lines to a python script 无法使用命令行参数将文件导入到python脚本并无法将print语句记录到另一个文件 - unable to import a file to a to a python script and to log the print statement to another file using command line argument 使用子进程的python脚本,将所有输出重定向到文件 - python script using subprocess, redirect ALL output to file Python-读取Excel文件并将输出打印到另一个文件 - Python - Read Excel file and print output to another file Python 在文件中记录 output - Python log output in a file 使用Tkinter标签中的exec从python中的列表中打印文件路径 - Print file paths from a list in python using exec in a tkinter label bash命令行记录txt文件python脚本 - bash command line to log txt file python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM