Perl - 自定义日志文件以仅显示特定错误

时间:2012-08-09 10:08:39

标签: perl

我是perl的新手,我们有一个类似于下面的日志文件:

SQL> @D:\Luntbuild_Testing\ASCMPK\Files\MAIN\DATABASE\HOST\FILES\DDL\20120412_152632__1_CLTM_EVENT_ACC_ROLE_BLOCK.DDL
SQL> CREATE TABLE CLTM_EVENT_ACC_ROLE_BLOCK
  2  (
  3  EVENT_CODE  VARCHAR2(4) ,
  4  ACC_ROLE  VARCHAR2(20)
  5  )
  6  ;
CREATE TABLE CLTM_EVENT_ACC_ROLE_BLOCK
             *
ERROR at line 1:
ORA-00955: name is already used by an existing object 


SQL> @D:\Luntbuild_Testing\ASCMPK\Files\MAIN\DATABASE\HOST\FILES\DDL\20120412_173845__2_CLTM_EVENT_ACC_ROLE_BLOCK.DDL
SQL> DROP TABLE  CLTM_EVENT_ACC_ROLE_BLOCK;

Table dropped.

现在我需要一个脚本来只显示有ORA-XXX错误的脚本路径,脚本应该只显示SQL的路径> @D:\ Luntbuild_Testing \与ORA-xxx错误相关,我试过下面的请你帮我加强一下。

$file = 'c:\data.txt';
open(txt, $file);
while($line = <txt>) {
print "$line" if $line =~ /> @/; #here i want the output to display the path of the script with only ORA-xxx errors and ignore if there are no errors
print "$line" if $line =~ /ORA-/;
}
close(txt); 

2 个答案:

答案 0 :(得分:1)

当您看到> @标记时,不是立即打印该行,而是将其存储在变量中,只有在您实际看到错误时才打印出来:

$file = 'c:\data.txt';
open(txt, $file);
while($line = <txt>) {
$fn = $line if $line =~ /> @/; #here i want the output to display the path of the script with only ORA-xxx errors and ignore if there are no errors
print $fn, $line if $line =~ /ORA-/;
}
close(txt);

另外:最好在脚本顶部写use strict;use warnings;use strict;强制您使用my明确命名您的本地变量,这会因拼写错误而导致很多错误。

答案 1 :(得分:1)

我会做一些与你尝试的非常相似的事情:

$file = 'c:\data.txt';
open(F, $file);
my $last_cmd = '';
while (<F>) {
  $last_cmd = $_ if /^SQL\> \@D:/;
  print $last_cmd if /^ORA-/;
}