-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogAnalyzer.py
150 lines (86 loc) · 3.5 KB
/
LogAnalyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#LogAnalyzer.py
#Python Script to search a file | return its current, previous and next line | write the obtained result i.e the current line into a new file.
while True:
fname1 = input("Enter Original File Name : ") #File Name 1 which contains the data to be SEARCHED.
fname2 = input("Enter New File Name : ") #File Name 2 in which you are going to store the RESULTS
try:
file1 = open(fname1) #Open File1
file2 = open(fname2, "w") #Open File2
except:
file1 = open(fname1+".txt")
file2 = open(fname2+".txt", "w")
lst = [] #List to store the lines of the file as elements
a = input("Enter the Text : ") #User Input
x = 0 #Line Counter
#Loop to read the entire file
#and store the lines of the file
#as elements in the list
#Also to find the length of the resultant List
#Which is the Total number of lines in the File
for k in file1:
lst.append(k)
list_length = len(lst)
#Main Loop
for i in lst:
x=x+1 #Line Counter starts to count
if(i.find(a)!=-1): #To check if user input exists in the line
ind = lst.index(i) #To find the index of the line in the list
before=ind-1 #To print previous line
after=ind+1 #To print after line
print("---------------------------------------------------------------------------------------------------")
print("***** Match found at line ",x,"*****")
print(" ")
print(" ")
print("*****Writing it to new file*****")
print(" ")
print(" ")
if not before ==-1: #to check If the index of previous line is -ve
print("Previous Line : ", lst[ind-1])
print(" ")
print(" ")
if before == -1:
print("No Previous Line")
print(" ")
print(" ")
print("Current Line : ", lst[ind])
print(" ")
print(" ")
if after<list_length: #to check If the index of after line is out of range in list
print("After Line : ", lst[ind+1])
print("-------------------------------------------------------------------------------------------------")
print(" ")
print(" ")
if not after<list_length:
print("End Of Line")
print(" ")
print(" ")
file2.write(i) #to write the results into a new file
else: #in case no match
print("No match in line ",x)
print(" ")
print(" ")
continue
#to close both the file
file1.close()
file2.close()
#TO QUIT THE TERMINAL
print("END OF THE SCRIPT, OPEN YOUR RESULT FILE TO FIND THE RESULTS")
print(" ")
print(" ")
print("Enter 0 to QUIT THE TERMINAL")
print(" ")
print(" ")
print("Enter 1 to CONTINUE")
print(" ")
print(" ")
quitt = int(input())
if quitt == 0:
quit()
elif quitt == 1:
continue
else:
print("INVALID INPUT!")
print(" ")
print(" ")
print("ABORTING...")
quit()