简体   繁体   English

迭代日志文件并使用正则表达式从行中提取时间值

[英]Iterate through a log file and use a regular expression to extract a time value from line

I have a perl script creates a text file, writes captured lines from a garbage collection log to it and saves the file in an archived/timestamped folder. 我有一个perl脚本创建一个文本文件,将捕获的行从垃圾收集日志写入它并将文件保存在一个存档/时间戳文件夹中。 The file will have lines that look like this: 该文件将具有如下所示的行:

413489.272: [GC [PSYoungGen: 323892K->3126K(332352K)] 623290K->303976K(1031424K), 0.0253970 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 413489.272:[GC [PSYoungGen:323892K-> 3126K(332352K)] 623290K-> 303976K(1031424K),0.0253970 secs] [次:用户= 0.03 sys = 0.00,real = 0.02 secs]

413503.531: [GC [PSYoungGen: 319094K->7280K(333760K)] 619944K->310249K(1032832K), 0.0614640 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 413503.531:[GC [PSYoungGen:319094K-> 7280K(333760K)] 619944K-> 310249K(1032832K),0.0614640 secs] [次:用户= 0.06 sys = 0.00,real = 0.06 secs]

413521.441: [GC [PSYoungGen: 324592K->6867K(333056K)] 627561K->310363K(1032128K), 0.0574120 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 413521.441:[GC [PSYoungGen:324592K-> 6867K(333056K)] 627561K-> 310363K(1032128K),0.0574120 secs] [次:用户= 0.06 sys = 0.00,real = 0.06 secs]

  ... 

What I'd like to do is iterate through these lines in the file, and use a regular expression to get the value of the "real" time (eg real=0.06 secs , but just the 0.06), and store it in a $time variable. 我想要做的是遍历文件中的这些行,并使用正则表达式来获取“实际”时间的值(例如, real=0.06 secs ,但只是0.06),并将其存储在$time变量。 I figure a positive lookbehind would work for this, something like /(?<=real=)\\d\\.\\d\\d/ , but that is not working. 我认为一个积极的lookbehind将适用于此,像/(?<= /(?<=real=)\\d\\.\\d\\d/ ,但这是行不通的。

In the end my script would look along the lines of: 最后,我的脚本将看起来像:

open LOG,"<","./report/archive/reports-$now/gclog.txt" or die "Unable to read file: $!";
    #while there is lines in the file
        #regex to match time value -> store in variable
        #print variable (just as a check for now)
        #some other stuff not really relevant to this question
close LOG;

I am fairly new to perl, any help would be greatly appreciated! 我对perl相当新,任何帮助都将不胜感激!

You don't need the negative look behind, just a capture. 你不需要背后的负面看,只需捕捉。

Use: 采用:

my ($time) = /\breal=([0-9.]+)/;

The \\b is probably not necessary, but I always prefer to match my word boundaries just in case. \\b可能没有必要,但我总是喜欢匹配我的单词边界以防万一。

The () cause it to capture the output, which is returned as an array. ()使它捕获输出,该输出作为数组返回。 I, then, put the returned value in the variable named $time . 然后,我将返回的值放在名为$time的变量中。 The capture value is also available in $1 , but I prefer to return it this way. 捕获值也是$1 ,但我更喜欢以这种方式返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM