2013-08-21 35 views
1

提取值下面是兩行,我有在文件中解析和from first line我需要提取"180000"第二線我需要提取"49 , 59, 54 "如何從字符串在Perl

Line 1: 
<Mon Aug 5 15:26:16:040.50 IST 2013> | <MNLOG> <Iteration : 1 Duration : 180000 seconds> 

Line2: 
<Mon Aug 5 15:26:16:044.93 IST 2013> | <MNLOG> <TestRunnerUnit - Remaining Time : 49 hours 59 min 54 sec > 

我已經寫了代碼,但我看起來很硬編碼一個。所以,如果有什麼我可以使用的方式,請讓我知道。

我的代碼是:

if($line =~ m/Iteration\s+:\s+1\s+Duration/) 
    { 
     print"$line\n"; 
     my @words = split(/ /,$line); 
     my $lengh = $#words; 
     $Start_Duration = $words[14]; 
    } 

if($line =~ m/Remaining\s+Time\s+:\s+\d+/) 
    { 
     my @words = split(/ /,$line); 
     my $lengh = $#words; 
     $Hours= $words[13]*3600; 
     $Minuts= $words[15]*60; 
     $Seconds= $words[17]; 
     $Remaining_Time =$Hours+$Minuts+$Seconds; 

    } 

登錄:

==================================================================================================== 
<Mon Aug 5 15:26:16:040.50 IST 2013> | <MNLOG> <Iteration : 1 Duration : 180000 seconds> 


==================================================================================================== 
<Mon Aug 5 15:26:16:042.11 IST 2013> | <MNLOG> <TestRunnerUnit - Total Weight : 34> 
<Mon Aug 5 15:26:16:042.88 IST 2013> | <MNLOG> <TestRunnerUnit - Total Objects : 34> 

<Mon Aug 5 15:26:16:043.87 IST 2013> | <MNLOG> <TestRunnerUnit - Random Number generated : 3> 
<Mon Aug 5 15:26:16:044.40 IST 2013> | <MNLOG> <TestRunnerUnit - Next Test : VideoStreamingSMS_APstress> 
<Mon Aug 5 15:26:16:044.93 IST 2013> | <MNLOG> <TestRunnerUnit - Remaining Time : 49 hours 59 min 54 sec > 
<Mon Aug 5 15:26:16:045.64 IST 2013> | <DBLOG> <TestRunnerUnit - Test Suite  : concurrency_tests> 
<Mon Aug 5 15:26:16:046.26 IST 2013> | <DBLOG> <TestRunnerUnit - Running the test : VideoStreamingSMS_APstress> 
<Mon Aug 5 15:26:16:046.81 IST 2013> | <DBLOG> <TestRunnerUnit - Timeout for the test : 1500> 
<Mon Aug 5 15:26:16:050.04 IST 2013> | <DBLOG> <TestRunnerUnit - Parameters  : 1> 
<Mon Aug 5 15:30:39:760.21 IST 2013> | <MNLOG> <TestRunnerUnit - Random Number generated : 13> 
<Mon Aug 5 15:30:39:760.69 IST 2013> | <MNLOG> <TestRunnerUnit - Next Test : CamcorderNoUsb_APstress> 
<Mon Aug 5 15:30:39:761.01 IST 2013> | <MNLOG> <TestRunnerUnit - Remaining Time : 49 hours 55 min 31 sec > 
<Mon Aug 5 15:30:39:761.28 IST 2013> | <DBLOG> <TestRunnerUnit - Test Suite  : nousb_tests> 
<Mon Aug 5 15:30:39:761.54 IST 2013> | <DBLOG> <TestRunnerUnit - Running the test : CamcorderNoUsb_APstress> 
<Mon Aug 5 15:30:39:761.77 IST 2013> | <DBLOG> <TestRunnerUnit - Timeout for the test : 1500> 
<Mon Aug 5 15:30:39:762.01 IST 2013> | <DBLOG> <TestRunnerUnit - Parameters  : 1> 
<Mon Aug 5 15:30:40:017.74 IST 2013> | <MNLOG> <TestRunnerUnit - Test Case : CamcorderNoUsb_APstress> 
<Mon Aug 5 15:30:40:018.47 IST 2013> | <DBLOG> <TestRunnerUnit - uses spiderboard: 0> 
<Mon Aug 5 15:32:34:136.59 IST 2013> | <MNLOG> <TestRunnerUnit - Random Number generated : 13> 
<Mon Aug 5 15:32:34:137.16 IST 2013> | <MNLOG> <TestRunnerUnit - Next Test : WallpaperToggle_APstress> 
<Mon Aug 5 15:32:34:137.60 IST 2013> | <MNLOG> <TestRunnerUnit - Remaining Time : 49 hours 53 min 36 sec > 
<Mon Aug 5 15:32:34:137.92 IST 2013> | <DBLOG> <TestRunnerUnit - Test Suite  : feature_tests> 
<Mon Aug 5 15:32:34:138.15 IST 2013> | <DBLOG> <TestRunnerUnit - Running the test : WallpaperToggle_APstress> 
<Mon Aug 5 15:32:34:138.38 IST 2013> | <DBLOG> <TestRunnerUnit - Timeout for the test : 1500> 
<Mon Aug 5 15:32:34:138.61 IST 2013> | <DBLOG> <TestRunnerUnit - Parameters  : 3> 
<Mon Aug 5 15:32:34:392.25 IST 2013> | <MNLOG> <TestRunnerUnit - Test Case : WallpaperToggle_APstress> 
<Mon Aug 5 15:32:34:392.97 IST 2013> | <DBLOG> <TestRunnerUnit - uses spiderboard: 0> 

<Mon Aug 5 15:32:36:395.53 IST 2013> | <MNLOG> <Installation - Waking the Device up and Unlocking it> 
<Mon Aug 5 15:32:36:396.17 IST 2013> | <DBLOG> <KeyMap_APstress - ADB keyevent : 6> 
<Mon Aug 5 15:33:36:407.53 IST 2013> | <DBLOG> <APStress_Command - Timeout on command: adb shell input keyevent 6> 
+1

這將是好多了,如果您發佈從日誌文件中較大的片段,而不會在日誌文件中您的意見。這將提供輸入格式的確切概念。 – Samveen

+0

請參閱http://perldoc.perl.org/perlre.html#Capture-groups – golimar

+0

您展示的較長示例中沒有「持續時間」行。 – Borodin

回答

2

這個程序應該可以幫助您。如果沒有看到完整的文件,就不可能知道需要多少驗證行,所以可能需要更精細的正則表達式來檢查是否正在處理正確的行。

在變量名中使用大寫字母是可疑的做法,因爲它們通常被保留用於Perl代碼中的常量,例如包名稱。

use strict; 
use warnings; 
use feature 'say'; 

my $start_duration; 
my ($hours, $minutes, $seconds); 
while (<DATA>) { 
    if (/Duration\s*:\s*(\d+)\s*seconds/) { 
    $start_duration = $1; 
    } 
    elsif (/Remaining Time\s*:\s*([^>]+)/) { 
    ($hours, $minutes, $seconds) = $1 =~ /\d+/g; 
    } 
} 

say $start_duration; 
say join ', ', $hours, $minutes, $seconds; 


__DATA__ 
<Mon Aug 5 15:26:16:040.50 IST 2013> | <MNLOG> <Iteration : 1 Duration : 180000 seconds> 
<Mon Aug 5 15:26:16:044.93 IST 2013> | <MNLOG> <TestRunnerUnit - Remaining Time : 49 hours 59 min 54 sec > 

輸出

180000 
49, 59, 54