views:

46

answers:

2

hi,

I am using this function in my code to return the strings i want from reading the log file, I want to grep the "exim" process and return the results, but running the code gives no error, but the output is limited to three lines, how can i just get the output only related to exim process..

#output:
    {'date': '13', 'process': 'syslogd', 'time': '06:27:33', 'month': 'May'}
    {'date': '13', 'process': 'exim[23168]:', 'time': '06:27:33', 'month': 'May'}
    {'May': ['syslogd']}


#function:    
def generate_log_report(logfile):
    report_dict = {}
    for line in logfile:
        line_dict = dictify_logline(line)
        print line_dict
        try:
            month = line_dict['month']
            date = line_dict['date']
            time = line_dict['time']
            #process = line_dict['process']
            if "exim" in line_dict['process']:
                process = line_dict['process']  
                break
            else:
                process = line_dict['process']      
        except ValueError:
            continue
        report_dict.setdefault(month, []).append(process)
    return report_dict
A: 

It's because you have a break statement inside the if that checks for "exim". As soon as you find a line with "exim", you will stop processing entirely, which sounds like the opposite of what you want!

I think you want to remove the break and put your printout inside the if. If your question is about the return value of the function, you need to make much more significant changes, probably removing report_dict entirely and simply creating a list of line_dicts that have exim in their process fields.

Personman
A: 

i changed the code to this, but it gives me just one line as output??? anything missing...

#!/usr/bin/env python

import sys

def generate_log_report(logfile):
        for line in logfile:
                line_split = line.split()
                list = [line_split[0], line_split[1], line_split[2], line_split[4]]
                if "exim" in list[3]:
                        l = [line_split[0], line_split[1], line_split[2], line_split[4]]
                else:
                        li = [line_split[0], line_split[1], line_split[2], line_split[4]]
        return l


if __name__ == "__main__":
        if not len(sys.argv) > 1:
                print __doc__
                sys.exit(1)
        infile_name = sys.argv[1]
        try:
                infile = open(infile_name, "r")
        except IOError:
                print "you must specify a valid file"
                print __doc__
                sys.exit(1)
        log_report = generate_log_report(infile)
        print log_report
        infile.close()
krisdigitx
i added"print l" before the else clause and that worked!, but i dont understand why "return l" did not work...
krisdigitx