1

I just want to know if you guys have better ways of doing this than the one I came up with. What I want is to make a "tail -f"-like script, but one that will look actively for a string and print only the text related to that string, in real time. As you can see from the code I'm looking for MAC addresses, but I guess it could be used for some other purposes.

I was thinking that there must be a better way of doing this. Maybe one of you guys knows a clever algorithm or a command that does this better. Thanks for your help

import time, os, sys
from datetime import date

# Function to get the file size, it will help us go to the end of a file
def current_file_size(filename):
    file_results = os.stat(filename)
    file_size = file_results[6]
    return file_size

# Check for correct usage
if len(sys.argv) != 2:
    print "Usage: %s <mac_address>" % sys.argv[0]
    sys.exit()

#Get the date in the format that the log uses
now = date.today()
todays_date = now.strftime("%Y%m%d")

#Set the filename and open the file
filename = 'complete.log'
file = open(filename,'r')

#Find the size of the file and move to the end
st_size = current_file_size(filename)
file.seek(st_size)

while 1:
    where = file.tell()   # current position of the file
    time.sleep(2)         # sleep for a little while
    st_size = current_file_size(filename)
    if st_size > where:       # if there's new text
        alotoflines = file.read(st_size-where)    # get the new lines as a group
        # search for the tag+mac address
        found_string = alotoflines.find("<mac v=\"" + sys.argv[1])
        if found_string > 0:
            # search for the immediately prior date instance from where the MAC address
            # is. I know that the log entry starts there
            found_date_tag = alotoflines.rfind(todays_date,0,found_string)
            print alotoflines[found_date_tag:]
perrocontodo
  • 113
  • 9
  • change `if st_size > where:` the variable `where` because thats actually a python builtin. – Jakob Bowyer May 12 '11 at 11:46
  • 1
    Also you can add document strings to your functions rather than comments. Remember Doc strings explain what it does, comments explain how. – Jakob Bowyer May 12 '11 at 11:56
  • 1
    have you checked out the solutions proposed here? http://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file-with-python-similar-to-tail – Sean Vieira May 12 '11 at 16:11
  • The question linked to by Sean is for doing something equivalent to 'tail'. What you're asking for though is 'tail -f' which is quite different behaviour. You might want to check out Tzury Bar Yochay's answer on this question, which I have found to work quite nicely: http://stackoverflow.com/questions/1475950/tail-f-in-python-with-no-time-sleep – Cam Jackson Aug 03 '11 at 00:32

1 Answers1

1

Are you doing this as a Python exercise or can you use the shell?

Can you simply pipe the tail output into a grep?

tail -F myfile.txt | egrep --line-buffered myPattern

You could put this into a script and make the file and pattern args.

Using grep you can also add context to your output by using the -A and -B switches.

Deano
  • 11
  • 1
  • I don't know before hand how many lines to output before or after the pattern match, so I wouldn't know what to put on the -A and -B switches – perrocontodo May 12 '11 at 12:10