Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5557 > unrolled thread
| Started by | J <jnr.gonzalez@googlemail.com> |
|---|---|
| First post | 2011-05-17 03:07 -0700 |
| Last post | 2011-05-17 06:33 -0700 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
Re: Convert AWK regex to Python J <jnr.gonzalez@googlemail.com> - 2011-05-17 03:07 -0700
Re: Convert AWK regex to Python AlienBaby <matt.j.warren@gmail.com> - 2011-05-17 06:33 -0700
| From | J <jnr.gonzalez@googlemail.com> |
|---|---|
| Date | 2011-05-17 03:07 -0700 |
| Subject | Re: Convert AWK regex to Python |
| Message-ID | <mailman.1664.1305626865.9059.python-list@python.org> |
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"
[toc] | [next] | [standalone]
| From | AlienBaby <matt.j.warren@gmail.com> |
|---|---|
| Date | 2011-05-17 06:33 -0700 |
| Message-ID | <12e7b8b8-20ac-40b1-bb66-3a9e22172b3f@d28g2000yqf.googlegroups.com> |
| In reply to | #5557 |
On May 17, 11:07 am, J <jnr.gonza...@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()
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web