![](/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.