我需要修复一些多行日志条目,目前使用perl,但我需要将功能移到python。
示例多行条目:
2015-12-02T17:56:13.783276Z our-elb-prod 52.20.50.51:60944 10.30.0.32:80 0.000024 0.063357 0.000066 200 200 0 12164 "GET http://www.example.com:80/episodes/2014/10/ HTTP/1.0" "IgnitionOneBot/Nutch-1.9 (
This is the IgnitionOne Company Bot for Web Crawling.
IgnitionOne Company Site: http://www.example.com/
;
rong2 dot huang at ignitionone dot com
)" - -
目前修复这些问题的perl脚本是:
while (my $row = <$fh>) {
chomp $row;
if ( $row =~ /^(\d{4})-(\d\d)-(\d\d)T(\d)/ ) {
print "\n" if $. != 1;
}
print $row;
输出更正后的单行条目:
2015-12-02T17:56:13.783276Z telepictures-elb-prod 52.20.50.51:60944 10.30.0.32:80 0.000024 0.063357 0.000066 200 200 0 12164 "GET http://www.example.com:80/episodes/2014/10/ HTTP/1.0" "IgnitionOneBot/Nutch-1.9 ( This is the IgnitionOne Company Bot for Web Crawling. IgnitionOne Company Site: http://www.example.com/ ; rong2 dot huang at ignitionone dot com )" - -
所以简而言之,我们基本上都在寻找任何不以日期正则表达式开头的行,如果它们匹配,我们将它们添加到第一行而没有\ n。
我已经看到了使用awk等完成此操作的其他方法,但需要将其作为纯Python。我看过Python. Join specific lines on 1 line,看起来像itertools可能是首选的方法吗?
答案 0 :(得分:1)
您可以使用基于负前瞻的正则表达式通过re
模块在python中实现此目的。
>>> import re
>>> s = '''2015-12-02T17:56:13.783276Z our-elb-prod 52.20.50.51:60944 10.30.0.32:80 0.000024 0.063357 0.000066 200 200 0 12164 "GET http://www.example.com:80/episodes/2014/10/ HTTP/1.0" "IgnitionOneBot/Nutch-1.9 (
This is the IgnitionOne Company Bot for Web Crawling.
IgnitionOne Company Site: http://www.example.com/
;
rong2 dot huang at ignitionone dot com
)" - -'''
>>> re.sub(r'\n(?!\d{4}-\d{2}-\d{2}T\d)', '', s)
'2015-12-02T17:56:13.783276Z our-elb-prod 52.20.50.51:60944 10.30.0.32:80 0.000024 0.063357 0.000066 200 200 0 12164 "GET http://www.example.com:80/episodes/2014/10/ HTTP/1.0" "IgnitionOneBot/Nutch-1.9 (This is the IgnitionOne Company Bot for Web Crawling.IgnitionOne Company Site: http://www.example.com/ ; rong2 dot huang at ignitionone dot com )" - -'
即
import re
with open(file) as f:
fil = f.read()
print re.sub(r'\n(?!\d{4}-\d{2}-\d{2}T\d)', '', s)