'Show lines of a file where an argument is found
I want to make a simple script where the user enters an argument and all the lines where the argument exists appear on the screen. This script will be used to make the search in one of my log files (which is very long and constantly updated) easier. I had thought of using grep for this, then I finally got the idea to change to glob which is better since we can work with several arguments.
But the code doesn't seem to work. When I try anything, nothing is displayed. I am a beginner in python and I can't see what exactly is wrong with my code.
In case the code works, it should show all lines in the log file where the given argument is present. But this is not the case.
import re
import sys
import glob
import os
if len(sys.argv) != 2:
print("Syntaxe: %s <UserID or IP Address>" % sys.argv[0].split('/')[-1])
sys.exit(1)
file = os.path.join("/applis/tacacs/log/tacacs.log")
for arg in sys.argv[2:]:
for file in glob.iglob(arg):
for line in open(file, 'r'):
if re.search(sys.argv[1], line):
print line,
Could someone please tell me what is wrong with my code? Thanks in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
