1

我有一个 perl 脚本创建一个文本文件,将捕获的行从垃圾收集日志写入它,并将文件保存在存档/时间戳文件夹中。该文件将包含如下所示的行:

413489.272: [GC [PSYoungGen: 323892K->3126K(332352K)] 623290K->303976K(1031424K), 0.0253970 秒] [时间: user=0.03 sys=0.00, real=0.02 secs]

413503.531: [GC [PSYoungGen: 319094K->7280K(333760K)] 619944K->310249K(1032832K), 0.0614640 秒] [时间: user=0.06 sys=0.00, real=0.06 secs]

413521.441: [GC [PSYoungGen: 324592K->6867K(333056K)] 627561K->310363K(1032128K), 0.0574120 秒] [时间: user=0.06 sys=0.00, real=0.06 secs]

                                       ...

我想做的是遍历文件中的这些行,并使用正则表达式来获取“实时”时间的值(例如real=0.06 secs,但只是 0.06),并将其存储在$time变量中。我认为积极的后视会为此工作,例如/(?<=real=)\d\.\d\d/,但这不起作用。

最后,我的脚本将如下所示:

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;

我对 perl 相当陌生,任何帮助将不胜感激!

4

1 回答 1

4

你不需要背后的负面看法,只需要捕捉。

采用:

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

可能不是必需的\b,但我总是更喜欢匹配我的单词边界以防万一。

()导致它捕获作为数组返回的输出。然后,我将返回的值放入名为 的变量中$time。捕获值在 中也可用$1,但我更喜欢以这种方式返回它。

于 2012-08-07T14:22:48.113 回答