Parsing a log file, etc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DDCane
    New Member
    • Oct 2007
    • 16

    Parsing a log file, etc

    i have to make 3 seperate functions that:

    function1. Counts the lines in a my 'logfile' and returns the answer in idle.

    function2. counts how many times a certain user is in the 'logfile' for instance how many times 'johnny.killed. peter' has been logged in the 'logfile' and returns only the answer

    function3. shows what files the user 'johnny.killed. peter' has looked at and writes the names of the files in a new file called 'pole3.out' with one name per row and it has to b ein alphabetical order with the most popular one first.

    number 3 can be a dictionary.

    thanks for all ur help guys u are really saving my life!
    Last edited by bartonc; Oct 26 '07, 04:59 AM.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by DDCane
    i have to make 3 seperate functions that:<snip>
    function2. counts how many times a certain user is in the 'logfile' for instance how many times 'johnny.killed. peter' has been logged in the 'logfile' and returns only the answer

    function3. shows what files the user 'johnny.killed. peter' has looked at and writes the names of the files in a new file called 'pole3.out' with one name per row and it has to b ein alphabetical order with the most popular one first.

    number 3 can be a dictionary.
    We'll actually need a sample of your log file for this.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by DDCane
      i have to make 3 seperate functions that:

      function1. Counts the lines in a my 'logfile' and returns the answer in idle.
      <snip>
      thanks for all ur help guys u are really saving my life!
      [CODE=python]
      >>> def NumLines(filena me):
      ... try:
      ... f = open(filename)
      ... nLines = len(f.readlines ())
      ... print 'There are %d lines in %s' %(nLines, filename)
      ... except IOError, error:
      ... print error
      ...
      >>> NumLines('modul e1.py')
      There are 165 lines in module1.py
      >>> NumLines('modul e1.p')
      [Errno 2] No such file or directory: 'module1.p'
      >>> [/CODE]

      Comment

      • DDCane
        New Member
        • Oct 2007
        • 16

        #4
        has anyone come up with anything on function 2 and 3? ive been fooling around with this for s couple days and cant seem to get it right. thanks for all the help.


        Originally posted by bartonc
        [CODE=python]
        >>> def NumLines(filena me):
        ... try:
        ... f = open(filename)
        ... nLines = len(f.readlines ())
        ... print 'There are %d lines in %s' %(nLines, filename)
        ... except IOError, error:
        ... print error
        ...
        >>> NumLines('modul e1.py')
        There are 165 lines in module1.py
        >>> NumLines('modul e1.p')
        [Errno 2] No such file or directory: 'module1.p'
        >>> [/CODE]

        Comment

        • DDCane
          New Member
          • Oct 2007
          • 16

          #5
          def countclient(fil ename, client):
          f = open(filename)
          x = len(f.readlines (client))
          return x

          does that look like something that could work?






          Originally posted by DDCane
          has anyone come up with anything on function 2 and 3? ive been fooling around with this for s couple days and cant seem to get it right. thanks for all the help.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by DDCane
            has anyone come up with anything on function 2 and 3? ive been fooling around with this for s couple days and cant seem to get it right. thanks for all the help.
            If we had a sample of your log file, I am sure we could show you how to get the data from it

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by DDCane
              does that look like something that could work?
              readlines() takes no arguments and it's good practice to close() th file when you are done with it.[CODE=python]def countclient(fil ename, client):
              f = open(filename)
              x = len(f.readlines ())
              f.close()
              return x[/CODE]
              Your posting is a bit out of hand. At 14 posts you are requited to follow all site rules (Posting Guidelines). Most perturbing is your lack of CODE tags. Instructions are right there on the right hand side of the page while you post.
              Last edited by bartonc; Oct 30 '07, 08:54 PM.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Originally posted by DDCane
                def countclient(fil ename, client):
                f = open(filename)
                x = len(f.readlines (client))
                return x

                does that look like something that could work?
                Function 2 could be something like this:[code=Python]user = 'bill.smith'
                logfile = 'log.txt'

                f = open(logfile)
                count = 0
                for line in f:
                if user in line:
                count += 1
                f.close()

                print count[/code]

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  As a function, it may look something like this:[code=Python]def log_count(fn, user):
                  f = open(logfile)
                  logList = [line for line in f if user in line]
                  f.close()

                  return logList

                  user = 'bill.smith'
                  logfile = 'log.txt'

                  logList = log_count(logfi le, user)
                  print len(logList)
                  [/code]

                  Comment

                  • ghostdog74
                    Recognized Expert Contributor
                    • Apr 2006
                    • 511

                    #10
                    Originally posted by bartonc
                    [CODE=python]
                    >>> def NumLines(filena me):
                    ... try:
                    ... f = open(filename)
                    ... nLines = len(f.readlines ())
                    ... print 'There are %d lines in %s' %(nLines, filename)
                    ... except IOError, error:
                    ... print error
                    ...
                    >>> NumLines('modul e1.py')
                    There are 165 lines in module1.py
                    >>> NumLines('modul e1.p')
                    [Errno 2] No such file or directory: 'module1.p'
                    >>> [/CODE]
                    just a minor note using readlines method. len(readlines() ) will count the extra blank line as a line,
                    eg
                    sample input:
                    line1
                    line2
                    line3

                    line4
                    line5
                    the above will produce 6. If that's what OP wants then its alright. this can be worked around by stripping off using .strip() :)

                    another method (out of the many) to count without including blank/new lines
                    Code:
                    for num,item in enumerate(open("file")): pass
                    print num

                    Comment

                    • ghostdog74
                      Recognized Expert Contributor
                      • Apr 2006
                      • 511

                      #11
                      Originally posted by DDCane
                      function2. counts how many times a certain user is in the 'logfile' for instance how many times 'johnny.killed. peter' has been logged in the 'logfile' and returns only the answer
                      Code:
                      >>> data=open("file").read()
                      >>> data.count("johnny.killed.peter")
                      you make your own function.

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #12
                        Originally posted by DDCane
                        def countclient(fil ename, client):
                        f = open(filename)
                        x = len(f.readlines (client))
                        return x

                        does that look like something that could work?
                        Now that you have some more information us, can you show us some more of your work? It would be best for you to attempt this on your own, then we can help from there.

                        Comment

                        • bvdet
                          Recognized Expert Specialist
                          • Oct 2006
                          • 2851

                          #13
                          Assume the visited page is in the format '....GET page_name HTTP....' in each line of the log file.[code=Python]import re

                          # Compile a list of entries for a specific 'user' in log file 'fn'
                          def log_count(fn, user):
                          f = open(logfile)
                          logList = [line for line in f if line.startswith (user)]
                          f.close()
                          return logList

                          # Create a dictionary using logList
                          # Dictionary keys are the web pages visited
                          # Dictionary values are the number of visits
                          def visit_count(log List):
                          patt = re.compile(r'GE T (.+) HTTP')
                          dd = {}
                          for log in logList:
                          page = patt.search(log ).group(1)
                          if page in dd:
                          dd[page] += 1
                          else:
                          dd[page] = 1
                          return dd

                          # Create list of value, key pairs from dictionary
                          # Sort on value (decending) and key (ascending)
                          # Disply sorted data
                          def visit_process(d d):
                          def comp(a, b):
                          x = cmp(a[0], b[0])
                          if not x:
                          return cmp(a[1], b[1])
                          return -x
                          pageList = zip(visitDict.v alues(), visitDict.keys( ))
                          pageList.sort(c omp)
                          for item in pageList:
                          print 'Page "%s" was visited %d time%s.' % (item[1], item[0], ['', 's'][item[0]>1 or 0])[/code]

                          Comment

                          Working...