我正在尝试简化实际工作的以下程序代码。目的是搜索要在文本文件(日志文件)中指定的字符串并输出这些行,但也输出此行之前和之后的5行。如果在匹配行后面的5行中再次出现搜索字符串,则必须避免这些行将被多次显示或导出。任何提示都非常感谢。
def analyze(search_string, outdev):
file = "infile.txt"
print "investigating file: " + file + " ..."
line1 = ""
line2 = ""
line3 = ""
line4 = ""
line5 = ""
line6 = ""
n = 0
output = 0
if outdev == "s": ### output: screen
data = open(file, "r")
for line in data.readlines():
line6 = line5
line5 = line4
line4 = line3
line3 = line2
line2 = line1
line1 = line
if line.find(search_string) != -1 and output == 0:
output = 1
print ""
print "-" * 80
print ""
print line6
print line5
print line4
print line3
print line2
print ""
print "." * 20
print ""
n = 1
changed = 1
found = 1
if output == 1 and 1 <= n <= 6 :
if line.find(search_string) != -1:
if found == 0:
changed = 1
else:
changed = 0
found = 1
else:
if found == 1:
changed = 1
else:
changed = 0
found = 0
if changed == 1:
print ""
print "." * 20
print ""
changed = 0
print line
n += 1
if line.find(search_string) != -1:
n = 1
if n == 6:
n = 0
output = 0
data.close()
if outdev == "f": ### output: file
out_file = "outfile.txt"
out_f = open(out_file, "w")
data = open(file, "r")
for line in data.readlines():
line6 = line5
line5 = line4
line4 = line3
line3 = line2
line2 = line1
line1 = line
if line.find(search_string) != -1 and output == 0:
output = 1
out_f.write(lf + "-" * 128 + lf + lf)
out_f.write(line6)
out_f.write(line5)
out_f.write(line4)
out_f.write(line3)
out_f.write(line2)
out_f.write(lf + "." * 20 + lf + lf)
n = 1
changed = 1
found = 1
if output == 1 and 1 <= n <= 6 :
if line.find(search_string) != -1:
if found == 0:
changed = 1
else:
changed = 0
found = 1
else:
if found == 1:
changed = 1
else:
changed = 0
found = 0
if changed == 1:
out_f.write(lf + "." * 20 + lf + lf)
changed = 0
out_f.write(line) # output recent line
n += 1 # increase counter
if line.find(search_string) != -1: # Search String found : reset counter
n = 1
if n == 6:
n = 0
output = 0
data.close()
out_f.close()
... 正如督察员G4dget所说: “changed”是一个标志,用于存储前一行的状态,以便控制行间的输出(“。”* 20)。 请查看评论以了解其工作原理:
if line.find(search_string) != -1: ## does the recent line contain the search string:
## => Yes:
if found == 0: ## was there a match in the previous line?
changed = 1 ## => No: the state has changed
else:
changed = 0 ## => Yes: the state didn't change
found = 1 ## store the state of the recent line in the found-flag
else: ## => No:
if found == 1: ## was there a match in the previous line?
changed = 1 ## => Yes: the state has changed
else:
changed = 0 ## => No: the state didn't change
found = 0 ## store the state of the recent line in the found-flag
答案 0 :(得分:0)
好吧,这就是我要做的事情:
class Buffer:
def __init__(self, size):
self.buffer = []
self.size = size
def add(self, elem):
if len(self.buffer) == self.size:
self.buffer.pop(0)
self.buffer.append(elem)
def getElems(self):
for elem in self.buffer:
yield elem
def analyze(search_string):
buffer = Buffer(5)
with open("infile.txt") as infile:
for line in infile:
line = line.strip()
if search_string in line:
if any(search_string in l for l in buffer): continue
print '-'*80
for e in buffer.getElems(): print e
print '.'*20
print line
buffer.add(line)
if __name__ == "__main__": analyze("search_string")
一旦你有了这个,那么你可以决定在哪里发送输出:
将其发送到屏幕:
$ python my_script.py
将其发送到文件:
$ python my_script.py > some_file.txt