我有两个我正在使用的文件,一个包含用户名列表。
$cat user.txt
johnsmith
MikeSmith
$cat logfile
root@host1 : /home/johnsmith
root@host2 : /home/johnsmith
root@host3 : /home/MikeSmith
日志日志文件包含跨多个主机的不同系统配置的转储,它还包括用户的主目录(如果有以下模式)。
如何迭代user.txt并查找/匹配包含用户名的任何/所有行。
码:
# 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
我不确定你想如何使用你的用户列表,但我想这可以用来在用户没有出现时引发错误。这将节省搜索不存在的用户日志的成本。
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
从磁盘读取它们。