【发布时间】:2015-04-06 18:34:53
【问题描述】:
我有来自应用服务器的日志,我需要获取带有“E”标志或“W”的行,如果在标记行之后有一行我也需要获取它。
我试着想出一个脚本:
#!/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/ @Sobrique :不,它会查找所有出现的模式
-
所以你想要的不仅仅是下一行,而是每场比赛后的任何非 [ 行?