Parse ASCII log ; sort and keep most recent entries
Nova's Taylor
novastaylor at hotmail.com
Fri Jun 18 13:51:42 EDT 2004
Wow - thanks for all of your great suggestions. I did neglect to
mention that the log file is appended to over time, so the values are
already in a time-sequenced sort going in, thus allowing the use of a
dictionary as suggested by David and others. This is what I wound up
using:
sourceFile = open(r'C:\_sandbox\SASAdmin\Python\ServerAdmin\SignOnLog.txt')
# output file for testing only
logFile = open(r'C:\_sandbox\SASAdmin\Python\ServerAdmin\test.txt',
'w')
piddict = {}
for line in sourceFile:
pid,username,date,time = line.split()
piddict[pid] = (username,date,time)
pidlist = piddict.keys()
pidlist.sort()
for pid in pidlist:
username,date,time = piddict[pid]
# next line seems amateurish, but that is what I am!
logFile.write(pid + " " + username + " " + date + "" + time +
"\n")
More background:
I will next merge this log file to process identifiers running on a
server, so I can identify "who-started-what-process-when." In Perl I
do it this way:
$pattern=sas; ## name of application I am searching for
# Use PSLIST.EXE to list processes on the server
open(PIDLIST, "pslist |") or die "Can not run the PSLIST program:
$!\n";
while(<PIDLIST>)
{
$output .=$_;
if (/$pattern/i)
{
## collect pids that match pattern into an array, splitting on
white spaces
@taskList=split(/\s+/, $_);
## Check each value in the Server task list with each row in the
log file
foreach $proc_val ( @fl )
{
chomp($proc_val); ## Remove new line characters at the end.
@log=split(/\s+/, $proc_val);
if ( $log[0] eq $taskList[1])
{
# print">>>>No matches in log Files!!<<<<<<<<<<< \n"; #
debug
print "$taskList[0] $log[0] $log[1] $log[2]
$taskList[5] $log[3] $taskList[8] \n";
$foundIt=1;
}
}
}
}
close(PIDLIST);
So now its more reading to see how to do this in Python!
Thanks again for all your help!
Taylor
More information about the Python-list
mailing list