Convert AWK regex to Python
MRAB
python at mrabarnett.plus.com
Mon May 16 12:39:11 EDT 2011
On 16/05/2011 09:19, J wrote:
[snip]
> #!/usr/bin/python
>
> # Import RegEx module
> import re as regex
> # Log file to work on
> filetoread = open('/tmp/ pdu_log.log', "r")
> # File to write output to
> filetowrite = file('/tmp/ pdu_log_clean.log', "w")
> # Perform filtering in the log file
> linetoread = filetoread.readlines()
> for line in linetoread:
> filter0 = regex.sub(r"<G_","",line)
> filter1 = regex.sub(r"\."," ",filter0)
> # Write new log file
> filetowrite.write(filter1)
> filetowrite.close()
> # Read new log and get required fields from it
> filtered_log = open('/tmp/ pdu_log_clean.log', "r")
> filtered_line = filtered_log.readlines()
> for line in filtered_line:
> token = line.split(" ")
> print token[0], token[1], token[5], token[13], token[20]
> print "Done"
>
[snip]
If you don't need the power of regex, it's faster to use string methods:
filter0 = line.replace("<G_", "")
filter1 = filter0.replace(".", " ")
Actually, seeing as how you're reading all the lines in one go anyway,
it's probably faster to do this instead:
text = filetoread.read()
text = text.replace("<G_", "")
text = text.replace(".", " ")
# Write new log file
filetowrite.write(text)
More information about the Python-list
mailing list