我有一个应用服务器的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