#!/bin/env python3 import re fp = open('./sample_auth_log2.txt', 'r') for line in fp: # 「Invalid user xxx from」のxxxの部分を # ()のグループに入れた正規表現で引っ掛ける。 match = re.search("Invalid user (.+) from", line) if match : print(match.group(1))