Convert AWK regex to Python

AlienBaby matt.j.warren at gmail.com
Tue May 17 09:33:25 EDT 2011


On May 17, 11:07 am, J <jnr.gonza... at googlemail.com> wrote:
> Hello,
>
> I have managed to get my script finished in the end by taking bits from everyone who answered.  Thank you so much.  the finished query string looks like this (still not the best but it gets the job done.  Once I learn to code more with Python I will probably go back to it and re-write it):-
>
> # Log file to work on
> filetoread = open("/tmp/pdu.log", "r")
> # Perform filtering in the log file
> text = filetoread.read()
> text = text.replace("<G_", "")
> text = text.replace(".", " ")
> text = text.replace(r"(", " ")
> filetoread.close()
> # File to write output to
> filetowrite = file("/tmp/pdu_filtered.log", "w")
> # Write new log file
> filetowrite.write(text)
> filetowrite.close()
> # Read new log and get required fields from it
> filtered_log =  open("/tmp/pdu_filtered.log", "r")
> filtered_line = filtered_log.readlines()
> for line in filtered_line:
>         field = line.split(" ")
>         field5 = field[5].rsplit("_", 1)
>         print field5[0], field[14], field[22]
> print "Done"

You can also process the lines and write them out to the new logfile
as you read them in first time around, rather than: read them in,
process them, write them out, read them in, process them, write them
out;

log_file=open("old_log_file","r")
output_file=open("new_log_file","w")
for line in log_file:
 line=line.replace("<G_", "").replace(".", " ").replace("(", " ")
 tokens=line.split()
 tokens_5=tokens[5].rsplit("_",1)
 output.file_write('%s %s %s\n' % (tokens_5,tokens[14],tokens[22]))
output_file.close()
log_file.close()



More information about the Python-list mailing list