我有一個應用服務器的ALOG,我需要得到與「E」標誌或「W」的線,如果有一條線後,我需要得到它。如何解析perl中的特定行?
我揣摩劇本:提前
#!/usr/bin/perl
use strict;
use warnings;
my $msg;
my $line;
my $line2;
main(@ARGV);
sub rec {
#$msg= $line;
print $line;
while ($line2 = <FH>) {
if ($line2 !~ m/^\[/){
#$msg = $msg.$line2;
print $line2;
} else {
if($line2 =~ m/ E | W /) { rec(); }
last;
}
}
#print $msg;
}
sub main {
open(FH,'SystemOut_15.02.05_17.00.02.log') or die "Error openong file : $!";
while ($line = <FH>) {
if($line =~ m/ E | W/and $line =~ m/^\[/){
rec();
}
}
close FH;
}
感謝。
樣品日誌:
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, S
QLState: null
*********************************************************** Start Server *******************************
0000003a JDBCException O OK
0000003a JDBCException O OK
********************************************************** End Server *******************************
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, S
QLState: null
org.hibernate.util.JDBCExceptionReporter
org.hibernate.util.JDBCExceptionReporter
org.hibernate.util.JDBCExceptionReporter
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException E org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, S
QLState: null
org.hibernate.util.JDBCExceptionReporter
org.hibernate.util.JDBCExceptionReporter
org.hibernate.util.JDBCExceptionReporter
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, S
QLState: null
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException E org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, S
QLState: null
org.hibernate.util.JDBCExceptionReporter
org.hibernate.util.JDBCExceptionReporter
org.hibernate.util.JDBCExceptionReporter
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK
什麼,我需要得到:
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, S
QLState: null
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, SQLState: null
org.hibernate.util.JDBCExceptionReporter
org.hibernate.util.JDBCExceptionReporter
org.hibernate.util.JDBCExceptionReporter
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException E org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, SQLState: null
org.hibernate.util.JDBCExceptionReporter
org.hibernate.util.JDBCExceptionReporter
org.hibernate.util.JDBCExceptionReporter
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, SQLState: null
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException E org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, SQLState: null
org.hibernate.util.JDBCExceptionReporter
org.hibernate.util.JDBCExceptionReporter
org.hibernate.util.JDBCExceptionReporter
一個忽略啓動和停止之間的線強方法是使用觸發器由YSTH指示;其他(弱)方法:
open(my $fh, '<', 'test2.log') or die "Error opening file : $!";
my $match = 0;
while (my $line = <$fh>) {
if ($line =~ /^\*+/){
$match = 0; ## initialize match if line start with star
}
if ($line =~ /^\[/) {
$match = $line =~ m/ E | W /;
}
print $line if $match;
}
close $fh;
這行代表單行(並在找到它時停止查找)?你能解釋一下你的代碼是做什麼的嗎?它看起來比你想描述的要複雜得多 – ysth 2015-02-06 15:25:19
你能提供一個輸入樣本和你期望提取的內容嗎? – Sobrique 2015-02-06 15:29:14
@ ysth/@Sobrique:不,它會查找所有發生的模式 – 2015-02-06 16:45:10