python在日志文件中找到匹配的字符串

时间:2019-03-21 16:51:58

标签: python-3.x

我有两个正在使用的文件,一个包含用户名列表。

$cat user.txt
johnsmith
MikeSmith
$cat logfile 
root@host1 : /home/johnsmith
root@host2 : /home/johnsmith
root@host3 : /home/MikeSmith

日志 logfile ,包含跨多个主机的不同系统配置的转储,还包括以下模式的用户主目录。

如何遍历 user.txt 并查找/匹配包含用户名的任何/所有行。

2 个答案:

答案 0 :(得分:0)

代码:

# Read User file
f = open("user.txt", "r")
names = f.read().split() # List of user names
f.close()
# Read Log file
f = open("logfile", "r") # List of log lines
log_lines = f.read().split('\n')
f.close()

for i, log in enumerate(log_lines):
    for name in names:
        if name in log:
            print(name + ' is present in line ' + str(i + 1))

输出:

johnsmith is present in line 1
johnsmith is present in line 2
MikeSmith is present in line 3

答案 1 :(得分:0)

我不确定您要如何使用用户列表,但是我想当用户未出现在列表中时,这可能会引起错误。这将节省搜索不存在的用户日志的成本。

import re

with open("user.txt") as f:
    users = set(f.read().splitlines())

config_pattern = re.compile(r"[^@]*@[\w]*\s*:\s*\/home\/(\w*)")

def find_user_configs(user_name):
    # We don't bother reading the file if the user doesn't exist:
    if user_name not in users:
        raise ValueError(f"User {user_name} doesn't exist.")

    with open("logfile") as f:
        for line in f:
            match = config_pattern.search(line)
            if match and match.groups()[0] == user_name:
                yield line.strip()


print(list(find_user_configs("johnsmith")))

这将打印johnsmith的配置列表:

['root@host1 : /home/johnsmith', 'root@host2 : /home/johnsmith']

请注意,根据您的需求,将所有日志放入内存中而不是每次find_user_configs都从磁盘中读取它们都是明智的做法。