2012-08-09 92 views
0

我是新来的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); 
+0

这将是巨大的,如果你的表演准确输入所需的输出。 – 2012-08-09 10:13:42

+0

输入将是从日志文件如上所示,我将读取日志文件, 输出我需要的是如下 SQL> @D:\ Luntbuild_Testing \ ASCMPK \文件\ MAIN \ DATABASE \ HOST \ FILES \ DDL \ 20120412_152632__1_CLTM_EVENT_ACC_ROLE_BLOCK .DDL ORA-00955:名称已经由现有对象 SQL> @D:\ Luntbuild_Testing \ ASCMPK \文件\升级-脚本\ POST_113 \典型\ misd_typ.sql ORA-02303:不能删除或替换键入类型或表格依赖项 – user1587062 2012-08-09 10:20:22

回答

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

我会做非常类似的东西给你试了一下:

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

非常感谢您的帮助 – user1587062 2012-08-13 07:14:59