[英]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.