![](/img/trans.png)
[英]Powershell Regular Expression to extract string between rounded parentheses from line in a log file
[英]Iterate through a log file and use a regular expression to extract a time value from line
我有一个perl脚本创建一个文本文件,将捕获的行从垃圾收集日志写入它并将文件保存在一个存档/时间戳文件夹中。 该文件将具有如下所示的行:
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] [次:用户= 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]
...
我想要做的是遍历文件中的这些行,并使用正则表达式来获取“实际”时间的值(例如, real=0.06 secs
,但只是0.06),并将其存储在$time
变量。 我认为一个积极的lookbehind将适用于此,像/(?<= /(?<=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相当新,任何帮助都将不胜感激!
你不需要背后的负面看,只需捕捉。
采用:
my ($time) = /\breal=([0-9.]+)/;
\\b
可能没有必要,但我总是喜欢匹配我的单词边界以防万一。
()
使它捕获输出,该输出作为数组返回。 然后,我将返回的值放在名为$time
的变量中。 捕获值也是$1
,但我更喜欢以这种方式返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.