Ultrin
Ultrin

Reputation: 43

Python 2.7.3: Search/Count txt file for string, return full line with final occurrence of that string

I'm trying to create a WiFi Log Scanner. Currently we go through logs manually using CTRL+F and our keywords. I just want to automate that process. i.e. bang in a .txt file and receive an output.

I've got the bones of the code, can work on making it pretty later, but I'm running into a small issue. I want the scanner to search the file (done), count instances of that string (done) and output the number of occurrences (done) followed by the full line where that string occurred last, including line number (line number is not essential, just makes things easier to do a gestimate of which is the more recent issue if there are multiple).

Currently I'm getting an output of every line with the string in it. I know why this is happening, I just can't think of a way to specify just output the last line.

Here is my code:

import os
from Tkinter import Tk
from tkFileDialog import askopenfilename

def file_len(filename):
    #Count Number of Lines in File and Output Result
    with open(filename) as f:
        for i, l in enumerate(f):
            pass     
    print('There are ' + str(i+1) + ' lines in ' + os.path.basename(filename))


def file_scan(filename):
    #All Issues to Scan will go here

    print ("DHCP was found " + str(filename.count('No lease, failing')) + " time(s).")
    for line in filename: 
        if 'No lease, failing' in line:
            print line.strip()

    DNS= (filename.count('Host name lookup failure:res_nquery failed') + filename.count('HTTP query failed'))/2
    print ("DNS Failure was found " + str(DNS) + " time(s).")
    for line in filename: 
        if 'Host name lookup failure:res_nquery failed' or 'HTTP query failed' in line:
            print line.strip()

    print ("PSK= was found " + str(testr.count('psk=')) + " time(s).")
    for line in ln:
        if 'psk=' in line:
            print 'The length(s) of the PSK used is ' + str(line.count('*'))



Tk().withdraw()
filename=askopenfilename()
abspath = os.path.abspath(filename) #So that doesn't matter if File in Python Dir
dname = os.path.dirname(abspath)    #So that doesn't matter if File in Python Dir
os.chdir(dname)                     #So that doesn't matter if File in Python Dir
print ('Report for ' + os.path.basename(filename))
file_len(filename)
file_scan(filename)

That's, pretty much, going to be my working code (just have to add a few more issue searches), I have a version that searches a string instead of a text file here. This outputs the following:

Total Number of Lines: 38
DHCP was found 2 time(s).
dhcp
dhcp
PSK= was found 2 time(s).
The length(s) of the PSK used is 14
The length(s) of the PSK used is 8

I only have general stuff there, modified for it being a string rather than txt file, but the string I'm scanning from will be what's in the txt files.

Don't worry too much about PSK, I want all examples of that listed, I'll see If I can tidy them up into one line at a later stage.

As a side note, a lot of this is jumbled together from doing previous searches, so I have a good idea that there are probably neater ways of doing this. This is not my current concern, but if you do have a suggestion on this side of things, please provide an explanation/link to explanation as to why your way is better. I'm fairly new to python, so I'm mainly dealing with stuff I currently understand. :)

Thanks in advance for any help, if you need any further info, please let me know.

Joe

Upvotes: 1

Views: 11564

Answers (2)

Nivir
Nivir

Reputation: 31218

To search and count the string occurrence I solved in following way

'''---------------------Function--------------------'''
#Counting the "string" occurrence in a file
def count_string_occurrence():
    string = "test"
    f = open("result_file.txt")
    contents = f.read()
    f.close()
    print "Number of '" + string + "' in file", contents.count("foo")
        #we are searching "foo" string in file "result_file.txt"

Upvotes: 7

William
William

Reputation: 3034

I can't comment yet on questions, but I think I can answer more specifically with some more information What line do you want only one of?

For example, you can do something like:

search_str = 'find me'
count = 0
for line in file:
    if search_str in line:
        last_line = line
        count += 1
print '{0} occurrences of this line:\n{1}'.format(count, last_line)

I notice that in file_scan you are iterating twice through file. You can surely condense it into one iteration :).

Upvotes: 1

Related Questions